Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to refer to a symbols' active record relationship inside a fields_for
    text
    copied!<p>Inside a fields_for block how can i reference the value of a relationship field.</p> <p>For instance:</p> <p><strong>app/models/cart.rb</strong></p> <pre><code>class Cart &lt; ActiveRecord::Base attr_accessible :lineitems_attributes has_many :lineitems, dependent: :destroy accepts_nested_attributes_for :lineitems def total_price lineitems.to_a.sum { |item| item.total_price } end end </code></pre> <p><strong>app/models/lineitem.rb</strong></p> <pre><code>class Lineitem &lt; ActiveRecord::Base attr_accessible :cart_id, :quantity, :package_id, :part_id belongs_to :cart belongs_to :package belongs_to :part def total_price if package_id? return package.price * quantity end if part_id? return part.price * quantity end end end </code></pre> <p><strong>app/models/package.rb</strong></p> <pre><code>class Package &lt; ActiveRecord::Base attr_accessible :description, :img_src, :name, :price has_many :lineitems end </code></pre> <p><strong>app/views/cart/_form.html.erb</strong></p> <pre><code>&lt;%= form_for @cart do |f| %&gt; &lt;%= c.fields_for :lineitems do |i| %&gt; &lt;%= render 'lineitem_fields', :f =&gt; i %&gt; &lt;% end %&gt; &lt;%= c.submit %&gt; &lt;% end %&gt; </code></pre> <p><strong>app/views/cart/_lineitem_fields.html.erb</strong></p> <pre><code>&lt;%= f.text_field :quantity %&gt; &lt;% if :package_id? %&gt; &lt;%= f.text_field :package_id %&gt; &lt;% else %&gt; &lt;%= f.text_field :part_id %&gt; &lt;% end %&gt; &lt;%= link_to 'Remove', lineitem_path(:id), :method =&gt; :delete, :confirm =&gt; t('.confirm', :default =&gt; t("helpers.links.confirm", :default =&gt; 'Are you sure?')) %&gt; </code></pre> <p><strong>relative pieces of schema</strong></p> <pre><code>create_table "carts", :force =&gt; true do |t| t.integer "branch_id" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end create_table "lineitems", :force =&gt; true do |t| t.integer "cart_id" t.integer "part_id" t.integer "package_id" t.integer "quantity", :default =&gt; 1 t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end create_table "parts", :force =&gt; true do |t| t.string "description" t.string "partNumber" t.decimal "price" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end create_table "packages", :force =&gt; true do |t| t.string "description" t.string "name" t.string "img_src" t.decimal "price" t.datetime "created_at", :null =&gt; false t.datetime "updated_at", :null =&gt; false end </code></pre> <p>The above form works but... </p> <p>Question 1: How to show the package.name instead of :package_id</p> <p>Question 2: How to display total_price of each lineitem in the form. It is a method how would that work?</p> <p>Question 3: Is there a best practice to displaying a form in an invoice looking manner where maybe the quantity is a text field but the remainder of the columns are just text or labels?</p> <p>The end game scenario is this form will be a last chance to edit the quantities of the cart (or remove lineitems) before submitting an order. Obviously in the real world you want to display the quantities, package name, description and price but I can't seem to figure out how to display those values inside the form since they are in another model by relationship and not specific to lineitems. </p> <p>Thanks for the help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload