我有三个模型:隔间、选项和产品,设置如下:
class Compartment < ActiveRecord::Base
has_and_belongs_to_many :products
has_and_belongs_to_many :options
...
end
class Option < ActiveRecord::Base
has_and_belongs_to_many :products
has_and_belongs_to_many :compartments
...
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :options
has_and_belongs_to_many :compartments
...
end
一个产品有很多隔间,隔间有很多选择。
我不能 100% 确定模型是否正确设置来执行我想要的操作。每当管理员创建产品时,他都应该看到创建的所有隔间和所有选项,这样他就可以选择可供最终用户使用的隔间和选项。每当用户登录并想要根据产品创建报价时,他应该使用管理员选择的选项来构建报价。一个简单的例子:
管理员 View :
- 产品A
- 隔间A
- 选项A
- 选项B
- 选项C
- B室
- 选项A
- 选项B
- 选项C
- 隔间A
用户 View :
- 产品A
- 隔间A
- 选项B
- 选项C
- B室
- 选项A
- 隔间A
隔间可以属于许多产品,选项也是如此,它们可以属于许多隔间。
我很难理解模型配置,最重要的是如何设置表单来保存/编辑/创建产品和报价。
再次感谢任何帮助!
最佳答案
我会在隔间和选项之间以及产品和隔间之间设置映射表并使用
Class Products
has_many :product_compartments
has_many :compartments, through: :product_compartments #Name of the map table
end
Class Compartment
has_many :product_compartments
has_many :products, through: :product_compartments #Name of the map table
has_many :compartment_options
has_many :options, through: :compartment_options #Name of the map table
end
Class Option
has_many :compartment_options
has_many :compartments, through: :compartment_options #Name of the map table
end
在您的管理界面中,您需要一个将产品连接到隔间的表单,可以像 2 个下拉菜单一样简单,一个包含产品,一个包含隔间。
以及类似的隔间和选项形式
然后您可以通过类似的方式为产品构建报价表单
<% @product.compartments.each do |compartment| %>
<%= #code rendering a checkbox for the compartment %>
<% compartment.options.each do |option| %>
<%= #code rendering a checkbox for the option %>
<% end %>
<% end %>
然后你可以用 javascript 来隐藏一些东西并使其成为一个漂亮的 UI。
关于mysql - 使用 Rails 3 的报价生成器应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26533645/