我有两种模式:
路线和活动
我通过类似的迁移实现了它们之间的多对多关系:
class ActivitiesRoutes < ActiveRecord::Migration
def up
create_table :activities_routes, :id => false do |t|
t.integer :route_id
t.integer :activity_id
end
end
end
在休息服务中,我获取路线的数据,我得到了多个活动,我的模型看起来像这样:
class Route < ActiveRecord::Base
attr_accessible :activities_attributes
has_and_belongs_to_many :activities
accepts_nested_attributes_for :activities
end
和
class Activity < ActiveRecord::Base
attr_accessible :activitytext, :iconid
has_and_belongs_to_many :routes
end
在我的应用程序控制器上,我想做类似的事情:
ruta=Route.create({
#other data for the model
})
ruta.activities_attributes = @activitiesarray #Array made with the Activities received
但是我收到了一个错误:
undefined method `activities_attributes' for #<Route:0x2bccf08>
如果我离开它:
ruta.activities_attributes << @activitiesarray
我明白了:
undefined method `with_indifferent_access' for #<Activity:0x6af7400>
有谁知道我可以做到这一点吗? 谢谢:))
答案 0 :(得分:1)
你不能这样做
ruta.activities_attributes << @activitiesarray
因为accepts_nested_attributes_for
仅提供*_attributes=
方法,所以以下内容应该有效
ruta.activities_attributes = @activitiesarray