PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] How to use has_many fields in views, rails?

 October 21, 2022     has-many, ruby-on-rails, views     No comments   

Issue

Frnds I am new to rails, here i created two tables caleed stocks and stock_availabilities.

in the stock model

class Stock < ActiveRecord::Base

  belongs_to :projects_lkp

  has_many :stock_availabilities

  validates_presence_of :item

end

In the stock_availabilities model

class StockAvailability < ActiveRecord::Base

  belongs_to :stock

  validates_presence_of :qty,:add_or_issue,:price, :captured_at, :stock_id,:unit

end

Now my doubt is how to bring the field of stock_availabilties in the views of stock

<% @stock.each do |d|  %>  
  <tr>
  <td><%= d.item %></td>

  "Here i need to print the values of qty and pricevwhich is in stock_availabilities class"? 

  </tr>

Solution

You are on the right track.

this is what you need:

<% @stock.each do |d|  %>  
  <tr>
  <td><%= d.item %></td>

  <% d.stock_availabilities.each do |sAV|  %>  
   <td> <%= sAV.qty %> </td>
       ...  <-- You do the other ones here
  <% end %>
  </tr>


Answered By - The Fabio
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing