Has Many Through. How to get the access?

Art B

Here is my models:

class Item < ActiveRecord::Base
has_many :price_group_lines
has_many :price_groups, through: :price_group_lines

attr_accessible :item_name, :item_id

validates :item_name, presence: true

def to_label
"#{item_name}"
end

end

class PriceGroup < ActiveRecord::Base
has_many :customers

has_many :price_group_lines
has_many :items, through: :price_group_lines

attr_accessible :price_group_name

validates :price_group_name, presence: true

def to_label
"#{price_group_name}"
end
end

class PriceGroupLine < ActiveRecord::Base
belongs_to :item
belongs_to :price_group

attr_accessible :item_id, :price_group_id, :price, :item, :price_group

validates :price, :item_id, :price_group_id, presence: true

end

View (show.html.erb) for Price Group controller

<h1> PRICE GROUP </h1>

<p> <%= @pg.price_group_name %> </p> <br>

<% @pg.items.each do |i|%>
<%= i.item_name %>
<% end %>
<br>

<%= link_to "PRICE GROUP List", price_groups_path %>

So, I have the access to item_name in show.html.erb, but i don't know how to get the access to "price" attribute in PriceGroupLine model. Something, like i.items.price not working. Please, help!

Nermin

Problem is because you have association of price, better said prices. Your variable i is already item just call price_group_lines on it and you will get association on which you can call each and get each price.

<% @pg.items.each do |i|%>
  <%= i.item_name %> # will display item name
  <% i.price_group_lines.each do |pgl| %>      
     <%= pgl.price %> # will display price
  <% end %>
<% end %>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

has_many 후 has_many : through?

분류에서Dev

has_one : through 및 has_many : through

분류에서Dev

link to relationship name in has_many :through

분류에서Dev

has_many : through 양식

분류에서Dev

Rails has_many : through with the where 절

분류에서Dev

Finding Users with has_many :through

분류에서Dev

Make has_many :through not deleteable

분류에서Dev

Join has_many :through attributes

분류에서Dev

Rails 4: has many through association error

분류에서Dev

has_many / : through rails4

분류에서Dev

Rails has_many : through, undefined method

분류에서Dev

Has-Many-Through 관계

분류에서Dev

How do I specify foreign_key and class when using has_many :through?

분류에서Dev

How to access internet and run apt-get through a middle server?

분류에서Dev

(discord.py) How can I get a list of how many permissions a user has

분류에서Dev

has_many : through와 has_and_belongs_to_many 중 선택- "should"또는 "must"

분류에서Dev

ActiveAdmin 필터 : has_many : through 속성

분류에서Dev

Can a has_many :through association have other associations?

분류에서Dev

Rails 3: has_many through controller action

분류에서Dev

Creating multiple has_many :through records with form_for

분류에서Dev

has_many : through로 사용자 찾기

분류에서Dev

NoMethodError: undefined method `user' for has_many :through Association

분류에서Dev

ActiveRecord, Rails 4 : has_many : through with scoped conditions failure

분류에서Dev

has_many through가있는 곳

분류에서Dev

Ruby on Rails has_many : through in a polymorphic association

분류에서Dev

rails cancancan cancan has_many through abilities

분류에서Dev

has_many through-속성 액세스

분류에서Dev

Rails has-many-through 연관 설정

분류에서Dev

How to add many-to-many relationships through a form?

Related 관련 기사

뜨겁다태그

보관