Rails 4: get data from many to many join table -


i did research. rails 3 use :select inject sql it, deprecated in rail 4.

how can archive easily?

for example in following models. want

boss.employees[0].salary 

models

class boss < activerecord::base     has_many :employments     has_many :employees, :through => :employments end  class employee < activerecord::base     has_many :employments     has_many :bosses, :through => :employments end 

employments migration:

class createemployments < activerecord::migration     def change       create_table :employments |t|         t.integer :boss_id         t.integer :employee_id         t.decimal :salary          t.timestamps       end     end end 

ok. found something.

class boss < activerecord::base   has_many :employments   has_many :employees, -> { select('employees.*, employment.salary salary') } :through => :employments end 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -