在我的服务器上,我有两个模型:
播送
class Broadcast < ActiveRecord::Base
validates_presence_of :content
belongs_to :user
has_and_belongs_to_many :feeds
attr_accessible :content, :feeds, :feeds_attributes
end
喂养
class Feed < ActiveRecord::Base
has_and_belongs_to_many :broadcasts
attr_accessible :name
end
在我的客户端上,我有这些模型的基本 ActiveResource 类。
当我尝试使用给定的提要(来自客户端)创建新的广播时:
feed = Feed.find(3) <-succesful
broadcast = Broadcast.new
broadcast.attributes['feed'] ||= []
broadcast.feed << feed
broadcast.save
在服务器上的 BroadcastController 中,我只是做
@broadcast = Broadcast.new(params[:broadcast])
它给出以下错误:
ActiveRecord::AssociationTypeMismatch (Feed(#45931224) 预期,得到 ActiveSupport::HashWithIndifferentAccess(#25685616)):
我试着改变
broadcast.attributes['feed'] ||= []
到
broadcast.attributes['feed_attributes'] ||= []
但它给了我“未知属性错误”</p>