アレイの問題を通してRailsのHAS_MANY上のルビー
-
12-09-2019 - |
質問
私は追加の問題を抱えているのはuser_ids
を使用しての関連を通じて多くを持っています。
私の通信モデルは次のようになります:
class communication
has_many :recipients
has_many :users, :through => :recipients
end
私は手動でそのような通信オブジェクトにuser_ids
を追加しようとしている通信コントローラのための私のcreateアクションで
@communication = new Communications(params[:communication])
@communication.user_ids << id
logger.debug @communication.user_ids # is empty
@communication.user_ids
配列が空である理由私はそうのようにハードコードされたIDを行う場合でも、うまくいかないことができます:
@communication = new Communications(params[:communication])
@communication.user_ids << 1
logger.debug @communication.user_ids # is still empty!
私はまだ空@communication.user_ids
配列を取得しています。
私は私の方法で何かが足りないのですか?この作業を取得するための任意のヒント?
事前に感謝します!
解決
それはhas_many :through
だので、多分あなたは関係がスムーズに作成できるように、完全なオブジェクトを指定する必要があります。これを試してください:
@communication = Communication.new params[:communication]
@communication.users << User.find( 1 )
@communication.user_ids # should be [ 1 ]
所属していません StackOverflow