A
A
Anton2016-05-02 20:53:22
Ruby on Rails
Anton, 2016-05-02 20:53:22

What is the problem with many-to-many relationships?

There are models: Employee, EmployeesPosition and EmployeesAndPosition.

class Employee < ActiveRecord::Base
  has_many :employees_and_positions
  has_many :employees_positions, through: :employees_and_positions, source: :employee
end

class EmployeesAndPosition < ActiveRecord::Base
  belongs_to :employee
  belongs_to :employees_position
end

class EmployeesPosition < ActiveRecord::Base
  has_many :employees_and_positions
  has_many :employees, through: :employees_and_positions
end

As a result, if you write: It
render json: Employee.find(1).employees_positions
will display everything about the employee, but not about his positions.
And in general, he doesn’t go there on a campaign connection:
#<Employee:0x007fecbfae2500>
What’s the problem?
I followed this example: rusrails.ru/active-record-associations#the-has-man...
It seems that I did everything right.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Demidenko, 2016-05-02
@hummingbird

take awaysource: :employee

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question