我有这些模型:轨道4:指定一个关系的记录,另一
ofert.rb
class Ofert < ActiveRecord::Base
has_many :purchasing_groups
end
purchasing_group.rb
class PurchasingGroup < ActiveRecord::Base
belongs_to :ofert
has_many :group_goals
end
group_goal.rb
class GroupGoal < ActiveRecord::Base
belongs_to :purchasing_group
end
在控制器动作我试图做这样的事情:
copy_of_last_group_goals = @ofert.purchasing_groups.last.group_goals
@ofert.purchasing_groups.create
#@ofert.last_purchasing_group.save! adding this, did not work
#@ofert.save! This neither work.
@ofert.purchasing_groups.last.group_goals = copy_or_last_group_goals
@ofert.save
以上的作品在本地很好,我也试过它从控制台,它也工作。我有我的应用程序部署在PostgreSQL的Heroku的,这不是在生产工作时,它抛出这个错误:
ActiveRecord::RecordNotSaved (Failed to replace group_goals because one or more of the new records could not be saved):
我试图拯救purchasing_groups
,也节省了ofert,你可以在注释行看到,但只在我的heroku应用程序的控制台中工作,但不是当我直接在部署的应用程序中尝试时。
任何想法?
为什么你只是不使用'a = @ ofert.purchasing_groups.create',然后'a.group_goals = copy_or_last_group_goals'? –
有什么区别?请帮我理解发生了什么=( – SsouLlesS
好吧,甚至复制'GroupGoal'实例也很奇怪,对于新的'PurchasingGroup',你必须创建新的组目标列表(根据你的模式),所以你不能复制数组,是因为你有唯一的'belongs_to:purchasing_group'关系,在其他情况下,你必须将你的模式更改为'PurchasingGroup'和'GroupGoal'之间的多对多关系 –