如何在视图中使用has_many字段,rails?

时间:2015-08-21 06:38:38

标签: ruby-on-rails views has-many

Frnds我是rails的新手,在这里我创建了两个表caleed stock和stock_availabilities。

在股票模型中

class Stock < ActiveRecord::Base

  belongs_to :projects_lkp

  has_many :stock_availabilities

  validates_presence_of :item

end

在stock_availabilities模型中

class StockAvailability < ActiveRecord::Base

  belongs_to :stock

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

end

现在我怀疑如何将stock_availabilties领域纳入股票的观点

<% @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>

1 个答案:

答案 0 :(得分:1)

你走在正确的轨道上。

这就是你需要的:

<% @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>