0

私は現在自分のコードを設定しているので、ユーザーはhas_many current_treatments(それらのユーザーとユーザーとの間の関連性がtrueに設定されたブール値の「current」を持つという点で他の処理とは異なります)。私が抱えている問題は、accepts_nested_attributes_forを介してネストされたフォームでユーザーの現在の治療法を指定しようとすると、「治療」が「現在の」ブール値セットで保存されていないことです。has_many宣言の条件に従うためにaccepts_nested_attributes_はありますか?

私はaccepts_nested_attributes_forを想定していましたが、これはあなたのために機能します。それはありませんか?もしそうなら、私は間違って何をしていますか?そうでない場合は、これを達成するための最良の方法は何ですか?ここで

は私の例です:

# user.rb 
    has_many :treatings 
    has_many :treatments, :through => :treatings 
    has_many :current_treatments, :through => :treatings, :conditions => {'treatings.current' => true}, :source => :treatment 

    accepts_nested_attributes_for :current_treatments 

そして、私は、ユーザーが経由して彼の現在の治療法を設定することができるようにしようとしている:

# user/edit.html.erb 
    <%= select_tag "user[current_treatment_ids][]", options_from_collection_for_select(Treatment.all, "id", "name", @user.current_treatment_ids), :multiple=>true %><br/> 

しかし、フォームの送信時に、私のような何かを得ます:

# development.log 
    SQL (0.4ms) INSERT INTO "treatings" ("created_at", "current", "treatment_id", "updated_at", "user_id") VALUES ('2011-01-15 18:49:02.141915', NULL, 4, '2011-01-15 18:49:02.141915', 1) 

新しい治療が指定iとtrueに設定し、「現在の」ブールなしで保存されることに注意してくださいn has_many宣言。

EDIT:ここにはTreatmentモデルがあります。

class Treatment < ActiveRecord::Base 

    has_many :treatings 
    has_many :users, :through => :treatings 
    has_many :current_users, :through => :treatings, :conditions => {:current => true}, :source => :user 
    has_many :past_users, :through => :treatings, :conditions => {:current => false}, :source => :user 

end 
+0

あなたの現在の治療モデルはどのように見えますか? – coreyward

+0

クラス治療:treatings にhas_many:current_users、:=>スルー:treatings:条件=> {:電流=>真} :source =>:ユーザー has_many:past_users、:through =>:扱い、:条件=> {:現在=>偽}、:ソース=>:ユーザー end –

+0

は私の "治療"モデルです。 「current_treatment」モデル自体はありません –

答えて

1

まあ、私は明白な問題を自分で見つけた:

# user/edit.html.erb 
<%= select_tag "user[current_treatment_ids][]", options_from_collection_for_select(Treatment.all, "id", "name", @user.current_treatment_ids), :multiple=>true %><br/> 

idでselect_tagの使用 "をユーザーが[current_treatment_ids] []" のためにaccepts_nested_attributesによって生成されたメソッドを呼び出すために行くことはありません。 idは "user [current_treatment_attributes] []"の範囲である必要があります。

しかし、私は実際にaccept_nested_attributes_for:current_treatmentか、代わりに:userと:current_treatmentという別名:current_treatingの間の協定の問題を提起していると思います。 Accepts_nested_attributes_for、私が理解しているように、新しいオブジェクトを作成するように設計されています。私はここで新しい治療法を創り出そうとしているのではなく、ただ新しい治療薬(2つの間の関連性)を創造しようとしています。

とにかく自分自身と少し話していますが、私はこの質問の私の望む方向がもはや私が望んでいたほど鋭いものではないので、これを解決してマークするつもりです。

関連する問題