associations

Rails 3 build a select tag with has_many belongs_to association

落爺英雄遲暮 提交于 2019-12-21 08:37:02
问题 Based on following models class Company < ActiveRecord::Base belongs_to :country end class Country < ActiveRecord::Base has_many :companies end I want to have in my companies/_form a select tag containing all the countries I think that the Company.new(params[:company]) in companies_controller#create can create the association between company and the selected country I'm running rails 3.0.0, what is the best way to achieve that? thanks for your insights 回答1: collection_select should do the

Pluck associated model's attribute in Rails query

蓝咒 提交于 2019-12-21 07:13:35
问题 In my rails app, collections have many projects , and projects have many steps . I'd like to grab all the ids of steps in a collection's projects, and I'm wondering if I can do it all in one query. For example, I know I can do the following step_ids = [] @collection.projects.each do |project| project.steps.each do |step| step_ids << step.id end end But is it possible to do something like the following: @collection.projects.include(:steps).pluck("step.id") // syntax here is not correct 回答1:

Rails: How do I model preferences? Should I use has_many :through?

丶灬走出姿态 提交于 2019-12-21 06:25:54
问题 I have two models: User and HairColor. A user has only one hair color, but can have many hair color preferences. What's the best way to model this? Here's what I started to do: #models/user.rb class User < ActiveRecord::Base belongs_to :hair_color has_many :preferences, has_many :hair_colors, :through => :preferences end #models/hair_color.rb class HairColor < ActiveRecord::Base has_many :users end #models/preference.rb class Preference < ActiveRecord::Base belongs_to :user belongs_to :hair

multicolumn primary keys in rails

柔情痞子 提交于 2019-12-21 05:09:12
问题 I'm trying to migrate a desktop application to rails (also dealing with quite old fashioned existing database). The problem is that I don't have a unique ID in one column, but it's three columns of a table that guarantee uniqueness of a record. Given I have three tables: authors author_name, author_letter, author_nr1, author_nr2 ... titles titel_nr, titel_name, ... author_titles titel_nr, author_letter, author_nr1, author_nr2 The "primary key" of authors consists of author_letter, author_nr1,

Rails has_many self referential

社会主义新天地 提交于 2019-12-21 02:45:13
问题 I have an accounts model as follows (simplified): class Account < ActiveRecord::Base attr_accessible :account_number, :display_name, :master_account_id has_many :child_accounts, :class_name => "Account", :foreign_key => "id" belongs_to :master_account, :class_name => "Account", :foreign_key => "master_account_id" end @account.master_account is currently working correctly, but I also want to be able to access @account.child_accounts - what do I need to do in order to fix that? 回答1: I think it

Rails: Why “has_many …, :through => …” association results in “NameError: uninitialized constant …”

北城以北 提交于 2019-12-20 18:03:11
问题 To express that a group can have multiple users, and a user can belong to multiple groups, I set the following associations: class Group < ActiveRecord::Base has_many :users_groups has_many :users, :through => :users_groups end class User < ActiveRecord::Base has_many :users_groups has_many :groups, :through => :users_groups end class UsersGroups < ActiveRecord::Base belongs_to :user belongs_to :group end However, when I type: Group.find(1).users I get: NameError: uninitialized constant Group

Trouble with accepts_nested_attributes_for on validating foreign key

浪尽此生 提交于 2019-12-20 16:23:33
问题 I am using Ruby on Rails v3.2.2. I would like to solve the issue related to the validation of a foreign key when using accepts_nested_attributes_for and validates_associated RoR methods. That is, I have following model classes: class Article < ActiveRecord::Base has_many :category_associations, :foreign_key => 'category_id' accepts_nested_attributes_for :category_associations, :reject_if => lambda { |attributes| attributes[:category_id].blank? } validates_associated :category_associations end

What inverse_of does mean in mongoid?

ぐ巨炮叔叔 提交于 2019-12-20 08:57:44
问题 What inverse_of does mean in mongoid associations? What I can get by using it instead of just association without it? 回答1: In a simple relation, two models can only be related in a single way, and the name of the relation is automatically the name of the model it is related to. This is fine in most cases, but isn't always enough. inverse_of allows you to specify the relation you are referring to. This is helpful in cases where you want to use custom names for your relations. For example:

Sails.js - Is there intended support for a “one-way-many” association

邮差的信 提交于 2019-12-20 03:19:12
问题 I'm interested in a one-way-many association. To explain: // Dog.js module.exports = { attributes: { name: { type: 'string' }, favorateFoods: { collection: 'food', dominant: true } } }; and // Food.js module.exports = { attributes: { name: { type: 'string' }, cost: { type: 'integer' } } }; In other words, I want a Dog to be associated w/ many Food entries, but as for Food , I don't care which Dog is associated. If I actually implement the above, believe it or not it works. However, the table

Tricky Questions Answers associations? [closed]

心已入冬 提交于 2019-12-20 03:12:50
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . So I have 3 models.. A User model, a Questions model and an Answer model. A user has_many questions, and questions belong_to user A question has_one answer, and the answers belong_to the question. Now I've created default seed questions that apply to all users i.e. @questions = Question.all And these same