using has_many :through and build

You can’t use a has_many :through like that, you have to do it like this: @company = Company.last @user = User.create( params[:user] ) @company.company_users.create( :user_id => @user.id ) Then you will have the association defined correctly. update In the case of the comment below, as you already have accepts_nested_attributes_for, your parameters would have to come … Read more

Rails has_many through form with checkboxes and extra field in the join model

Looks like I figured it out! Here’s what I got: My models: class Product < ActiveRecord::Base has_many :categorizations, dependent: :destroy has_many :categories, through: :categorizations accepts_nested_attributes_for :categorizations, allow_destroy: true validates :name, presence: true def initialized_categorizations # this is the key method [].tap do |o| Category.all.each do |category| if c = categorizations.find { |c| c.category_id == category.id … Read more

Ruby-on-Rails: Multiple has_many :through possible?

Edit: Rails 3.1 supports nested associations. E.g: has_many :tasks has_many :assigments, :through => :tasks has_many :users, :through => :assignments There is no need for the solution given below. Refer to this screencast for more details. Original Answer You are passing a has_many :through association as a source for another has_many :through association. I don’t think … Read more