私は現在自分のコードを設定しているので、ユーザーは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
あなたの現在の治療モデルはどのように見えますか? – coreyward
クラス治療:treatings にhas_many:current_users、:=>スルー:treatings:条件=> {:電流=>真} :source =>:ユーザー has_many:past_users、:through =>:扱い、:条件=> {:現在=>偽}、:ソース=>:ユーザー end –
は私の "治療"モデルです。 「current_treatment」モデル自体はありません –