activerecord - Rails 4 - Displaying items from database tables -
i have following models , associations:
#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   i want display item.name, order_item.quantity , order_item.price in 1 table attempted below:
  <tbody>     <% @order.items.each |item| %> <<<<<<<< need call item.name       <% @order.order_items.each |order_item| %> <<<<<<< need call other fields         <tr>           <td><%= item.name %></td>           <td><%= order_item.quantity %></td>           <td><%= order_item.price %></td>         </tr>       <% end %>     <% end %>   </tbody>   the above works in calling specific field how written not because inner loop needs finish before outer , hence don't need.
#tables snippets
create_table "items", force: true |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 |t|     t.integer  "item_id"     t.integer  "order_id"     t.integer  "quantity"     t.decimal  "price"   end   create_table "orders", force: true |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) |f| %>  <%= f.association :client, collection: client.all, label_method: :name, value_method: :id, prompt: "client name", required: true %>   <%= f.simple_fields_for :items |o| %>   <%= o.input :name, collection: product.all, label_method: :name, value_method: :id %>   <%= o.input :quantity %>  <% end %>   <%= f.button :submit %> <% end %>      
you may wish use .delegate method orderitem model, this:
#app/models/order_item.rb class orderitem < activerecord::base     belongs_to :item     belongs_to :order     delegate :name, to: :item #-> allows call @order_item.name  end   this allow call:
<tbody>     <% @order.order_items.each |item| %>         <tr>           <td><%= item.name %></td>           <td><%= item.quantity %></td>           <td><%= item.price %></td>         </tr>       <% end %> </tbody>   fix
a much better way make work use following model names:
#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 call @item.name  end   this allow call:
<tbody>     <% @order.items.each |item| %>         <tr>           <td><%= item.name %></td>           <td><%= item.quantity %></td>           <td><%= item.price %></td>         </tr>     <% end %> </tbody>      
Comments
Post a Comment