rails-models

Query that joins child model results item erroneously shown multiple times

旧巷老猫 提交于 2019-12-08 06:13:06
问题 I have the following models, each a related child of the previous one (I excluded other model methods and declarations for brevity): class Course < ActiveRecord::Base has_many :questions scope :most_answered, joins(:questions).order('questions.answers_count DESC') #this is the query causing issues end class Question < ActiveRecord::Base belongs_to :course, :counter_cache => true has_many: :answers end class Answer < ActiveRecord::Base belongs_to :question, :counter_cache => true end Right now

Ruby on Rails - concatenate strings on save to database upon user clicking create action

混江龙づ霸主 提交于 2019-12-06 15:16:02
问题 Its late and I probably should sleep on this. Easy one I have 3 fields in a form which a user fills in. Once they click the create button these records are saved to the database. Simple. However I want the data from these three fields at the same time to be concatenated together, nothing fancy..and inserted to the database at the same time as the other records. This should reflect back to the user on the show page after they create. So I need an action to concatenate 3 db columns lets say

Rails associations - how can I set up associations for different varieties of users?

旧城冷巷雨未停 提交于 2019-12-06 12:18:58
问题 I'm creating a web app which consists of schools, courses, students, and teachers. A school can have many courses and a course has one teacher and many students. The problem I am running into is that a single user could be a teacher of one course, but a student in another course (or even a student or teacher in a course in a different school). I don't want to create a model for teachers and a separate model for students because I would like to track all my users in one place. There is an

nested has_many :through in rails 3

廉价感情. 提交于 2019-12-06 05:11:56
问题 I know Rails doesn't support nested has_many :through relationships, though there's been talk and an open ticket about a patch since as early as Rails 2. I did come across a plugin that's pretty slick, but the master branches don't work with Rails 3 and I'm hesitant to use it for mission critical tasks in the app hence the lack of active recent development. So -- what's the best way to deal with these relations. class Author < ActiveRecord::Base has_many :contracts has_many :products,

Ruby on Rails pass id to new create form

╄→гoц情女王★ 提交于 2019-12-05 21:54:50
Ok, I've searched high and low, read tutorials, watched videos and I am still not getting any where with this. I've read similar questions here, but questions were more complex or lacked answers - so here goes... I have models Account and Invoice. When showing an Account, I'd like a link to 'Create new invoice' which relates to that account. (Later I'd actually like a select field to choose an Account when creating an Invoice, but I'll leave that to another excruciation). Here are my models... Account: class Account < ActiveRecord::Base attr_accessible :name, :invoice attr_accessible :name,

Ruby on Rails - concatenate strings on save to database upon user clicking create action

牧云@^-^@ 提交于 2019-12-04 19:21:32
Its late and I probably should sleep on this. Easy one I have 3 fields in a form which a user fills in. Once they click the create button these records are saved to the database. Simple. However I want the data from these three fields at the same time to be concatenated together, nothing fancy..and inserted to the database at the same time as the other records. This should reflect back to the user on the show page after they create. So I need an action to concatenate 3 db columns lets say column names are firstname, surname and DOB. table name PeopleDetails I have tried building the model

nested has_many :through in rails 3

僤鯓⒐⒋嵵緔 提交于 2019-12-04 10:22:47
I know Rails doesn't support nested has_many :through relationships, though there's been talk and an open ticket about a patch since as early as Rails 2. I did come across a plugin that's pretty slick, but the master branches don't work with Rails 3 and I'm hesitant to use it for mission critical tasks in the app hence the lack of active recent development. So -- what's the best way to deal with these relations. class Author < ActiveRecord::Base has_many :contracts has_many :products, :through => :contracts class Product < ActiveRecord::Base has_many :contracts has_many :orders has_many

Rails Updating Data in one Model from another Model's Controller

元气小坏坏 提交于 2019-12-03 22:47:17
I have a User model that has billing_id. I have Order model that runs transactions with a payment gateway which returns a billing id I'd like to save into the billing_id column on the User model. I think I am getting the basics of MVC architecture mixed up. I am under the impression that UsersController and OrdersController update data of their respective tables. Does that mean that if I have something returned from OrdersController such as a billing id, there is no other way of saving that billing id into a billing_id column in User model? Thanks and sorry if this is extremely rudimentary. :)

How to add sequences to a migration and use them in a model?

你离开我真会死。 提交于 2019-12-03 19:47:26
问题 I want to have a " Customer " Model with a normal primary key and another column to store a custom "Customer Number". In addition, I want the db to handle default Customer Numbers. I think, defining a sequence is the best way to do that. I use PostgreSQL. Have a look at my migration: class CreateAccountsCustomers < ActiveRecord::Migration def up say "Creating sequenze for customer number starting at 1002" execute 'CREATE SEQUENCE customer_no_seq START 1002;' create_table :accounts_customers

has_one and has_many in same model. How does rails track them?

本秂侑毒 提交于 2019-12-03 09:45:18
问题 I a little confused about how this work even if it works properly. I have a model that has two association to the same other model. Company has an owner and company has many employees of the class users. here is my company model: class Company < ActiveRecord::Base validates_presence_of :name has_many :employee, :class_name => 'User' has_one :owner, :class_name => 'User' accepts_nested_attributes_for :owner, :allow_destroy => true end here is my user model: class User < ActiveRecord::Base