0

を使用してHasManyThroughアソシエーションに追加する際に問題が発生しましたuser_ids

私のコミュニケーションモデルは次のようになります。

class communication
 has_many :recipients
 has_many :users, :through => :recipients
end

通信コントローラーの作成アクションで、次のuser_idsように通信オブジェクトに手動で追加しようとしています。

@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配列を取得しています。

私の方法で何かが欠けていますか?これを機能させるためのヒントはありますか?

前もって感謝します!

4

1 に答える 1

1

has_many :throughなので、関係をスムーズに作成できるように、完全なオブジェクトを提供する必要があるかもしれません。これを試して:

@communication = Communication.new params[:communication]
@communication.users << User.find( 1 )
@communication.user_ids  # should be [ 1 ]
于 2009-09-21T04:49:20.583 に答える