Rails 4 - 显示数据库表中的项目

时间:2014-06-10 00:54:39

标签: ruby-on-rails activerecord ruby-on-rails-4 simple-form

我有以下模型及其关联:

#models

class Order < ActiveRecord::Base
    has_many :order_items
    has_many :items, :through => :order_items
    accepts_nested_attributes_for :items, :order_items, :allow_destroy => true
end
class Item < ActiveRecord::Base
    has_many :order_items
    has_many :orders, :through => :order_items
end
class OrderItem < ActiveRecord::Base
    belongs_to :item
    belongs_to :order
end

我想在下表中尝试在一个表格中显示item.nameorder_item.quantityorder_item.price

  <tbody>
    <% @order.items.each do |item| %> <<<<<<<< need this to call item.name
      <% @order.order_items.each do |order_item| %> <<<<<<< need this to call the other fields
        <tr>
          <td><%= item.name %></td>
          <td><%= order_item.quantity %></td>
          <td><%= order_item.price %></td>
        </tr>
      <% end %>
    <% end %>
  </tbody>

上述工作在调用特定字段但是如何编写它不会做,因为内部循环需要在外部完成,因此我们不能得到我们需要的东西。

#Tables片段

create_table "items", force: true do |t|
    t.string   "name"
    t.decimal  "price"
    t.integer  "stock"
    t.string   "location"
    t.decimal  "discount"
    t.boolean  "status"
  end
  create_table "order_items", force: true do |t|
    t.integer  "item_id"
    t.integer  "order_id"
    t.integer  "quantity"
    t.decimal  "price"
  end
  create_table "orders", force: true do |t|
    t.string   "code"
    t.integer  "user_id"
    t.text     "memo"
    t.boolean  "status"
    t.integer  "client_id"
    t.decimal  "sub_total"
  end

#orders form

<%= compact_form_for(@order) do |f| %>
 <%= f.association :client, collection: Client.all, label_method: :name, value_method: :id, prompt: "Client Name", required: true %>

 <%= f.simple_fields_for :items do |o| %>
  <%= o.input :name, collection: Product.all, label_method: :name, value_method: :id %>
  <%= o.input :quantity %>
 <% end %>

 <%= f.button :submit %>
<% end %>

1 个答案:

答案 0 :(得分:1)

您可能希望将.delegate方法与OrderItem模型一起使用,如下所示:

#app/models/order_item.rb
class OrderItem < ActiveRecord::Base
    belongs_to :item
    belongs_to :order
    delegate :name, to: :item #-> allows you to call @order_item.name 
end

这将允许您致电:

<tbody>
    <% @order.order_items.each do |item| %>
        <tr>
          <td><%= item.name %></td>
          <td><%= item.quantity %></td>
          <td><%= item.price %></td>
        </tr>
      <% end %>
</tbody>

<强>修正

更多更好的方法是使用以下模型名称:

#app/models/product.rb
class Product < ActiveRecord::Base
    has_many :items
    has_many :orders, :through => :items
end

#app/models/order.rb
class Order < ActiveRecord::Base
    has_many :items
    has_many :products, :through => :items
    accepts_nested_attributes_for :products, :allow_destroy => true
end

#app/models/item.rb
class Item < ActiveRecord::Base
    belongs_to :product
    belongs_to :order
    delegate :name, to: :product #-> allows you to call @item.name 
end

这将允许您致电:

<tbody>
    <% @order.items.each do |item| %>
        <tr>
          <td><%= item.name %></td>
          <td><%= item.quantity %></td>
          <td><%= item.price %></td>
        </tr>
    <% end %>
</tbody>