2011-08-11 13 views
0

著者に製品を接続する結合モデルがあります。それは契約と呼ばれています。私はすぐに製品が作成される契約を作成したいので、私の製品モデルに私が持っている:レールでの結合テーブルの問題

 after_save :create_contract 

    def create_contract 
     contract = Contract.new(
      :product_id => self.id, 
      :author_id => @author_id 
     ) 
     contract.save 
    end 

それは私には十分に単純に見えた、しかし:それはに行く準備ができていたときに、常にnilを起動したAUTHOR_IDデータベース。私はそれを設定するいくつかの異なる方法を試してきました、何も動作するようです。

<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> 

とコントローラで:私はこのようになりますこれは、それは私が製品の形でそれを提出していますどのように関連しています推測している。ここ

def create 
    @author_id = params[:contract][:author_id] 
    @product = Product.new(params[:product]) 
    ... 
    end 

は私が見ているものですログ。

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]] 

問題のどこに問題がありますか?

class Product < ActiveRecord::Base 
    has_many :authors, :through => :contracts 

class Author < ActiveRecord::Base 
    has_many :products, :through => :contracts 

class Contract < ActiveRecord::Base 
    belongs_to :author 
    belongs_to :product 
end 
+0

そして、私はそれを、追加したいですインスタンス変数とは対照的に、author_idを直接モデルに設定すると、それはまだゼロになっていました。 – Slick23

+0

あなたが質問を編集したい場合は、それを更新することができます:あなたが作った完全なモデルを表示することができますか?私はそこに何か間違ったことがあると思う。 – Veger

+0

コメントボタンを押すだけですばやくできました。私は関連付けを追加しました。 – Slick23

答えて

0

create_contractであなたの@author_idは、あなたのコントローラと同じスコープ内にそのためではないProductモデルであり、。

class Product < ActiveRecord::Base 
    has_one :contract, :dependent => :destroy 
    has_one :author, :through => :contract 
    accepts_nested_attributes_for :contract 
end 

class Contract < ActiveRecord::Base 
    belongs_to :product 
    belongs_to :author 
end 

class Author < ActiveRecord::Base 
    has_many :contracts, :dependent => :destroy 
    has_many :products, :through => :contracts 
end 

次にフォームで:

はあなたのモデルに次のように入れてみてください

... 
<%= f.fields_for :contract do |c| %> 
    <%= c.collection_select :author_id, Author.all, :id, :name %> 
<% end %> 
... 

これを試してみて、それがどのようになる参照してください。製品のコントローラでは、

<%= f.fields_for :contract do |c| %> 
    <%= c.collection_select :author_id, Author.all, :id, :name %> 
<% end %> 

<%= form_for(@product) do |f| %>と仮定して)、その後

と::

+0

このようにして契約を結んでいるわけではありません。 – Slick23

+0

これは機能します...しかし私自身の答えで私の追加を見ます – Slick23

1

ので、jimworm's nested_attributesは2回交換して、作品

def new 
    @product = Product.new 
    contract = @product.contracts.build 
...