2009-09-21 25 views
0

我在添加到有很多通過協會使用user_ids的問題。Ruby on Rails有許多通過陣列問題

我的溝通模式是這樣的:

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 

我可以」 t解決爲什麼@communication.user_ids陣列是空的,即使當我這樣做一個硬編碼的ID:

@communication = new Communications(params[:communication]) 
@communication.user_ids << 1 
logger.debug @communication.user_ids # is still empty! 

我仍然收到一個空的@communication.user_ids數組。

我錯過了我的方法嗎?任何提示讓這個工作?

在此先感謝!

回答

2

由於這是一個has_many :through,也許你需要提供完整的對象,以便可以順利地創建關係。試試這個:

@communication = Communication.new params[:communication] 
@communication.users << User.find(1) 
@communication.user_ids # should be [ 1 ]