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

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -