如何将属性保存到has_many:通过连接表,没有要构建的现有记录
我有一个表单,使用accepts_nested_attributes_for
创建一个新的子记录和一个新的父记录
孩子和父母有一个has_many :through
协会:
class Child < ActiveRecord::Base has_many :relationships has_many :parents, through: :relationships accepts_nested_attributes_for :parents accepts_nested_attributes_for :relationships, allow_destroy: true end class Parent < ActiveRecord::Base has_many :relationships has_many :children, through: :relationships end class Relationship < ActiveRecord::Base belongs_to :child belongs_to :parent accepts_nested_attributes_for :parents #has :schedule attribute end
我想同时在Relationships中设置schedule
属性,并且不能使用accepts_nested_attributes_for
因为我正在操作控制器中的某些数据以在保存之前确定schedule
对象。
堆栈溢出有很多答案可以通过预先存在的父记录完成此操作,但是我找不到任何先前没有关联的情况。
我试图在模型中使用after_create
回调来执行此操作,但是无法访问params
并且已经读过这样做是不好的程序。 ( Rails如何将params从控制器传递到模型内的after_save )。
理想情况下,我想在相同的数据库调用中设置schedule
,以创建关系记录以及child_id
和parent_id
。
对此有任何帮助都很棒,谢谢
UPDATE
控制器作为原始post
def new @child = Child.new end def create @child = Child.new(params[:child]) schedule = build_schedule(params) # Need to save schedule to @child.relationship[0].schedule somehow if @child.save! redirect_to admins_path else render 'new' end end private def build_schedule(params) # works with params[:mon, :tue, :wed, :thu, :fri] to return a schedule object. end
parent
是使用Ryan Bates的嵌套表单助手构建的。 这是它的构建线:
module ApplicationHelper def link_to_add_fields(name, f, association) new_object = f.object.send(association).klass.new ... end end
我根本没有建立这种relationship
,据我所知,它是由铁轨神奇地建造的。
尝试Danny Van Hoof的建议@child.relationships[0].schedule =
我得到undefined method schedule=' for nil:NilClass
因为没有关系存在。
如果我事先使用@child.relationship.build
在new
或create
action中构建关系,我会保存2条关系记录:
我构建的那个已经保存了schedule
但没有parent_id
(parental_group_id)第二个记录是一个神奇的构建,并且具有正确的ID但没有schedule
。
现在通过添加来尝试Rick Peck的建议
def params params.require(:child).permit(relationships_attributes: [:schedule]) # tried with and without `parents_attributes` end
我在params.require
行上得到了一个stack level too deep
错误。 我也不完全明白这是做什么的。 (我对铁杆很新)
谢谢你的帮助!
我在我的一个网站上有类似的构造,它工作正常,除了我(翻译成你的例子)
attr_accessible :relationships_attributes accepts_nested_attributes_for :relationships, allow_destroy: true
在我的关系模型中,然后我可以访问parent_id
这允许在控制器中执行类似的操作
@child.relationships[0].schedule = ...
甚至在拯救孩子之前
或者,也可以在输入父详细信息时立即从视图中设置计划
如果不清楚,请给我发表评论!
首先,您的accepts_nested_attributes_for
必须包含join model
,然后将数据传递到其他模型,如下所示:
class Child < ActiveRecord::Base has_many :relationships has_many :parents, through: :relationships accepts_nested_attributes_for :relationships end class Parent < ActiveRecord::Base has_many :relationships has_many :children, through: :relationships end class Relationship < ActiveRecord::Base belongs_to :child belongs_to :parent #attr_accessor :schedule ? accepts_nested_attributes_for :parents end #app/controllers/child_controller.rb def new @child = Child.new @child.relationships.build.parents.build end private def params params.require(:child).permit(relationships_attributes: [:schedule, parents_attributes: [:variable-1, :variable-2]]) end
这会将所需数据传递给relationships
,然后传递给parents
。 然后,当您处理relationships
模型中的变量时,可以处理该schedule