简体   繁体   English

通过其他模型在Rails中创建嵌套模型

[英]Creating nested models in rails, through an other model

I've a stock, and a calving model. 我有股票,还有产犊模型。
A stock (has an id, name, and a mother_id) can have carves through a calving, so I have this in my stock model. 一只股票(有一个id,名字和一个mother_id)可以通过产犊来雕刻,所以我的股票模型中有这个。

    has_many :stocks, through: :calvings
    has_many :calvings

In Calving model i have: 在产犊模型中,我有:

    has_many :calves, class_name: "Stock", foreign_key: "mother_id", 
    accepts_nested_attributes_for :calves

I have a form for creating Calving, where the user can: 我有一个创建Calving的表格,用户可以在其中:

-choose the stock_id (it will be the mother) -选择stock_id(它将是母亲)
-add how many carves was born -加上出生的雕刻数
-give a name for every carf (depending on the nr of carves born) -为每条小牛皮取个名字(取决于所生小牛皮的编号)

I would like to save this data, but when it creates the Stock record, it puts the carving.id into the stock.mother_id field, not the carving.stock_id. 我想保存此数据,但是当它创建Stock记录时,它将graving.id放到stock.mother_id字段中,而不是graving.stock_id。

How can i fix this? 我怎样才能解决这个问题?

And if it is possible, what should I do, to fill the form with the right data when I want to edit the record? 如果有可能,当我要编辑记录时,应该怎么做才能用正确的数据填写表格?

has_many :through has_many:通过

If you're creating records in a has_many :through association, you first need to create the records for the join model, then pass the records through the "child" model 如果要在has_many :through关联中创建记录,则首先需要为联接模型创建记录,然后将记录通过“子”模型传递

Here's how we do it: 这是我们的方法:


Models 楷模

#app/models/stock.rb
Class Stock < ActiveRecord::Base
    has_many :calvings, class_name: "Calving"
    has_many :calves, class_name: "Calf", through: :calvings

    accepts_nested_attributes_for :calving

    #build relevant objects (DRYes up controller)
    def self.build 
        stock = self.new
        stock.calvings.build.build_calf
        stock
    end
end

#app/models/calving.rb
Class Calving < ActiveRecord::Base
   belongs_to :stock
   belongs_to :calf 

   accepts_nested_attributes_for :calf
end

#app/models/calf.rb
Class Calf < ActiveRecord::Base
   has_many :calvings, class_name: "Calving"
   has_many :stocks, class_name: "Stock", through: :calvings
end

stocks #needs to identify mother only
id | name | created_at | updated_at

calvings
id | stock_id | calf_id | extra | info | created_at | updated_at

calfs
id | name | extra | information | created_at | updated_at

Controllers 控制器

These models give you a structure where you'll have a join model (which holds the relation between stocks and calves , allowing you to populate them as required: 这些模型为您提供了一个结构,其中将具有联接模型(该模型保留了stockscalves之间的关系,允许您根据需要填充它们:

#app/controllers/stocks_controller.rb
def new
    @stock = Stock.build
end

def create
    @stock = Stock.new(stock_params)
    @stock.save
end

private

def stock_params
    parmas.require(:stock).permit(:name, calvings_attributes: [calf_attributes:[:info]]
end

Forms 形式

Finally, you'll be able create the form as follows: 最后,您将能够如下创建表单:

#app/views/stocks/new.html.erb
<%= form_for @stock do |f| %> 
    <%= f.name %>
    <%= f.fields_for :calvings do |c| %>
        <%= c.fields_for :calf do |calf| %>
            <%= calf.text_field :info %>
        <% end %>
    <% end %>
<% end %>

Update 更新

Rails, self-referential association on the User model to define friends/followers Rails,用户模型上的自引用关联,用于定义朋友/关注者

Might be able to do it like this: 可能可以这样做:

#app/models/stocks.rb
Class Stock < ActiveRecord::Base
   has_many :calvings
   has_many :calves, class_name: "Stock", through: :calvings
end

#app/models/calvings.rb
Class Calving < ActiveRecord::Base
   belongs_to :mother, :class_name => "Stock", foreign_key: "mother_id"
end

I'm not actually sure about this to be honest -- needs more thought. 老实说,我实际上并不确定-需要更多考虑。 Typically, you'd use a standard has_many :through relationship for it. 通常,您将使用标准的has_many :through关系。 I'll email to get a call going - we need to fix this!!! 我会通过电子邮件发送电话-我们需要解决此问题!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM