I have a join model that connects Authors to Products. It's called contracts. I want to create the contract as soon as the product is created, so in my product model I have:
after_save :create_contract
def create_contract
contract = Contract.new(
:product_id => self.id,
:author_id => @author_id
)
contract.save
end
It seemed simple enough to me, but :author_id always comes up nil when it's ready to go into the database. I've tried a couple different ways of setting it, nothing seems to work. I'm guessing it's related to how I'm submitting it with the products form, which looks like this:
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :handle %><br />
<%= f.text_field :handle %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :keywords %><br />
<%= f.text_field :keywords %>
</div>
<div>
<%= collection_select( "contract", "author_id", @authors, "id", "full_name") %>
</div>
And in the controller:
def create
@author_id = params[:contract][:author_id]
@product = Product.new(params[:product])
...
end
Here's what I'm seeing in the log.
Parameters: {"utf8"=>"✓", "authenticity_token"=>"...", "product"=>{"title"=>"", "handle"=>"", "description"=>"", "keywords"=>""}, "contract"=>{"author_id"=>"1"}, "commit"=>"Create Product"}
SQL (1.1ms) INSERT INTO "products" ("created_at", "description", "handle", "keywords", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["created_at", Mon, 08 Aug 2011 04:37:09 UTC +00:00], ["description", ""], ["handle", ""], ["keywords", ""], ["title", ""], ["updated_at", Mon, 08 Aug 2011 04:37:09 UTC +00:00]]
SQL (0.7ms) INSERT INTO "contracts" ("author_id", "created_at", "product_id", "updated_at") VALUES (?, ?, ?, ?) [["author_id", nil], ["created_at", Mon, 08 Aug 2011 04:37:09 UTC +00:00], ["product_id", 5], ["updated_at", Mon, 08 Aug 2011 04:37:09 UTC +00:00]]
Any ideas on where the problem is?
class Product < ActiveRecord::Base
has_many :authors, :through => :contracts
and
class Author < ActiveRecord::Base
has_many :products, :through => :contracts
and
class Contract < ActiveRecord::Base
belongs_to :author
belongs_to :product
end