使用嵌套模型创建与多对多关系的条目 [英] Form to create entries in many-to-many relationship with nested models

查看:221
本文介绍了使用嵌套模型创建与多对多关系的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序由以下五种模式组成:
超市可以有不同的类别产品和这些类别中的产品可以由几个品牌制作



现在我想要一个(或两个)选择在我的超市 -form中,我可以在 Category 中选择一个元素,并显示其名称以及 Brand 中的一个或多个元素>的名字出现,所以这可以存储在 Origin 中。



我想我可以使用 collection_select code>,但我如何在这里使用它?

  class Supermarket< ActiveRecord :: Base 
has_many:耗材
has_many:origins,:through => :耗材
结束

类供应< ActiveRecord :: Base
belongs_to:origin
belongs_to:超市
结束

类别Origin< ActiveRecord :: Base
belongs_to:类别
belongs_to:品牌
结束

类别< ActiveRecord :: Base
has_many:origin
end

class Brand< ActiveRecord :: Base
has_many:origin
end

可能我也有调整模型...



编辑



澄清表单的结果应该是什么:



在编辑 Supermarkets 的表单中,我想选择 品牌,以便 超市(表格):
$超市(表格):
超市
b $ b

超市的名称沃尔玛






类别(选择一个):




  • 可乐(Category_ID 1) li>
  • 玉米片(Category_ID 2)

  • ...






品牌(多选)


  • 可口可乐公司(Brand_ID 1)

  • 百事可乐公司(Brand_ID 2)

  • 凯洛格公司 Brand_ID 3)

  • ...





 
Supermarket_ID Category_ID Brand_ID
1 1 1
1 1 2
2 1 2
2 2 3
... ... ...

/编辑




编辑2 b $ b

根据这个问题,我可以像这样创建或选择一个条目通过控制台

  walmart = Supermarket.create(:name => 沃尔玛); 
cornflakes = Category.create(:name =>Corn Flakes);
kellogs = Brand.create(:name =>Kellog's);

walmart.origins.create(:category_id =>玉米片,:brand_id =凯洛格)

如何通过表单使用此功能?我如何利用选择 collection_select 或者是否有其他帮助者可以使用?



/编辑2

解决方案

我也有类似的问题当采用多对多的关系时。一个值得检查的宝石将是 simple_form

他们为to_many关联提供了一个很好的表单助手。

就实际执行表单而言,我不确定我是否理解表单数据如何使用。无论如何,JavaScript和虚拟属性都是你的朋友。



编辑: 啊,我明白你在说什么了。我会这样做的方式是创建一个select_field,用于选择类别,然后创建另一个select_field(设置:multiple => true),以便我可以选择多个品牌。

 <%= select_tagsupermarket [supplies] [] [category]...%> 
<%= select_tag超市[用品] [] [品牌],...%> <! - 务必将多个设置为true! - >

接下来,您需要将所有内容关联在一起。为此,我将创建一个虚拟属性来处理表单提交。例如:

  def supplies =(supplies_hash)
#读取supplies_hash并创建活动记录。
结束

def用品

结束



最后,如果你坚持这样做没有宝石,我建议你看看嵌套表单railscast。以了解涉及的内容。

My application consists of these five models: A supermarket can have different categories of products and the products in these categories can be produced by several brands.

Now I want to have one (or two) Selection-field(s) in my supermarket-form in which I can select one element in Category with its name appearing and one or more element(s) in Brand with its name appearing, so this could be stored in Origin.

I think I could use collection_select, but how do I utilize it here?

class Supermarket < ActiveRecord::Base
  has_many :supplies
  has_many :origins, :through => :supplies
end

class Supply < ActiveRecord::Base  
  belongs_to :origin  
  belongs_to :supermarket  
end

class Origin < ActiveRecord::Base
  belongs_to :category
  belongs_to :brand
end

class Category < ActiveRecord::Base
  has_many :origins
end

class Brand < ActiveRecord::Base
  has_many :origins
end

Probably, I also have to tweak the models...


Edit

To clarify what the outcome of the form should be:

In the form to edit Supermarkets I want to select a Category of products and the corresponding Brands so that I know, which Category and which Brands in this Category is/are sold in this specific Supermarket:

Supermarket (Form):

Name of Supermarket: Walmart


Category (Select one):

  • Cola (Category_ID 1)
  • Cornflakes (Category_ID 2)
  • ...

Brand (multiple Select)

  • The Coca-Cola Company (Brand_ID 1)
  • PepsiCo (Brand_ID 2)
  • Kellogg Company (Brand_ID 3)
  • ...

This should create entries in Origin like:

Supermarket_ID Category_ID Brand_ID 
      1            1          1 
      1            1          2 
      2            1          2 
      2            2          3 
     ...          ...        ... 

/Edit


Edit 2

According to the answer to this question, I could create or select an entry via console like this:

walmart = Supermarket.create(:name => "Walmart");
cornflakes = Category.create(:name => "Corn Flakes");
kellogs = Brand.create(:name => "Kellog's");

walmart.origins.create(:category_id => cornflakes, :brand_id = kellogs)

How can I make use of this via a form? How could I utilize select or collection_select or is there even another helper I could use?

/Edit 2

解决方案

I have also had a similar problem when utilizing many-to-many relationships. One worthwhile gem to check out would be simple_form.

They have a nice form helper for to_many associations.

In terms of actually implementing the form, I'm not sure I understood how the form data will be used. Regardless, javascript and virtual attributes are your friends here.

EDIT:

Ah, I see what you're saying. the way I would do this would be to create one select_field, for selecting the category, then I would create another select_field (setting :multiple => true) so I can select multiple brands.

<%= select_tag "supermarket[supplies][][category]" ... %>
<%= select_tag "supermarket[supplies][][brands]", ... %> <!-- be sure to set multiple to true !-->

Next, you need to associate everything together. So for that, I would create a virtual attribute that handles the form submission. For example:

def supplies=(supplies_hash)
# read supplies_hash and create the active records. 
end

def supplies

end

Finally, if you're insisting on doing this without a gem, I recommend you take a look at the nested form railscast. to get an idea of what's involved.

这篇关于使用嵌套模型创建与多对多关系的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆