mysql - 使用 Rails 3 的报价生成器应用

标签 mysql ruby ruby-on-rails-3 associations

我有三个模型:隔间、选项和产品,设置如下:

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

用户 View :

  • 产品A
    • 隔间A
      • 选项B
      • 选项C
    • B室
      • 选项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/

相关文章:

ruby-on-rails-3 - Rails - 为什么 `accepts_nested_attributes_for` 将 `autosave` 设置为 true

MySQL简单程序错误

mysql - 从 MySql 数据库中截断或删除表

mysql - : "Column count doesn' t match value count at row 1"这个错误是什么原因

html - 预编译后 html 图像路径是否仍然有效?

ruby-on-rails-3 - 如何使用 Twitter Bootstrap 实现响应式 Youtube 嵌入 iframe?

php - 使用 laravel 按纬度和经度搜索

javascript - rails : Anchor Tag to jump to location on same page without sending a new request and having to re-render the page

ruby - 如何在 Rails 控制台(或 irb)中输入多字节字符?

ruby-on-rails-3 - Rescu 的日志记录问题