Ruby on Rails的HAS_MANY通过数组问题
-
12-09-2019 - |
题
我在加入到问题的有许多通过强>使用关联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
阵列。
我失去了我的方法什么?任何提示得到这个工作?
提前感谢!
解决方案
因为它是一个has_many :through
,也许你需要提供完整的对象,这样的关系可以顺利创建。尝试这样:
@communication = Communication.new params[:communication]
@communication.users << User.find( 1 )
@communication.user_ids # should be [ 1 ]
不隶属于 StackOverflow