Join has_many :through attributes

tomasbasham

I have what I though to be a very simple set of database models with a many-to-many type association through a linker table.

class Product < ActiveRecord::Base
  has_many :store_products
  has_many :stores, through: store_products
end

class StoreProduct < ActiveRecord::Base
  belongs_to :store
  belongs_to :product

  validates :price, presence: true
end

class Store < ActiveRecord::Base
  has_many :store_products
  has_many :products, through: :store_product
end

So many stores can sell many products and can each sell them at different prices. I have been looking for a way to list all products along with their lowest price across all stores using joins. I have had next to no luck with this. The best I have had was being able to make a query that returned bulbs for the lowest selling price (I think) but the price attribute was not included in the output.

The query I used to do this was:

Product.joins(:store_products).select('products.*, MIN(store_products.price) AS store_product_price')

Any suggestions on where I am going wrong or what I need to take a look at?

Rustam A. Gasanov

If your query works fine, you can access store_product_price. To see it, just try this:

Product.joins(:store_products)
       .select('products.*, MIN(store_products.price) AS store_product_price')
       .each { |p| puts p.store_product_price }

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

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

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

has_many 후 has_many : through?

분류에서Dev

has_many through 및 join 테이블이있는 간단한 양식

분류에서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

has_many / : through rails4

분류에서Dev

Rails has_many : through, undefined method

분류에서Dev

has_one : through 및 has_many : through

분류에서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

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

분류에서Dev

validates_uniqness를 사용하여 has_many_through에 대한 nested_attributes_for 허용

분류에서Dev

ordering through an has_many relationship with a condition in rails 3 / active records / postgresql

분류에서Dev

has_many : through 잘못된 것 같습니다

분류에서Dev

Has_many, Through : Joiner 테이블 저장 실패

분류에서Dev

FactoryGirl: has_many :through, validates_presence_of: association error: can't be blank?

분류에서Dev

ActiveRecord has_many through inverse_of causing rails admin to work?

분류에서Dev

Having trouble adding view created from a has_many: through table.

Related 관련 기사

  1. 1

    has_many 후 has_many : through?

  2. 2

    has_many through 및 join 테이블이있는 간단한 양식

  3. 3

    link to relationship name in has_many :through

  4. 4

    has_many : through 양식

  5. 5

    Rails has_many : through with the where 절

  6. 6

    Finding Users with has_many :through

  7. 7

    Make has_many :through not deleteable

  8. 8

    has_many / : through rails4

  9. 9

    Rails has_many : through, undefined method

  10. 10

    has_one : through 및 has_many : through

  11. 11

    ActiveAdmin 필터 : has_many : through 속성

  12. 12

    Can a has_many :through association have other associations?

  13. 13

    Rails 3: has_many through controller action

  14. 14

    Creating multiple has_many :through records with form_for

  15. 15

    has_many : through로 사용자 찾기

  16. 16

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

  17. 17

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

  18. 18

    has_many through가있는 곳

  19. 19

    Ruby on Rails has_many : through in a polymorphic association

  20. 20

    rails cancancan cancan has_many through abilities

  21. 21

    has_many through-속성 액세스

  22. 22

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

  23. 23

    validates_uniqness를 사용하여 has_many_through에 대한 nested_attributes_for 허용

  24. 24

    ordering through an has_many relationship with a condition in rails 3 / active records / postgresql

  25. 25

    has_many : through 잘못된 것 같습니다

  26. 26

    Has_many, Through : Joiner 테이블 저장 실패

  27. 27

    FactoryGirl: has_many :through, validates_presence_of: association error: can't be blank?

  28. 28

    ActiveRecord has_many through inverse_of causing rails admin to work?

  29. 29

    Having trouble adding view created from a has_many: through table.

뜨겁다태그

보관