I have these models:
class Student < ActiveRecord::Base has_many :tickets has_many :movies, through: :tickets end class Movie < ActiveRecord::Base has_many :tickets, dependent: :destroy has_many :students, through: :tickets belongs_to :cinema end class Ticket < ActiveRecord::Base belongs_to :movie, counter_cache: true belongs_to :student end class Cinema < ActiveRecord::Base has_many :movies, dependent: :destroy has_many :students, through: :movies has_many :schools, dependent: :destroy has_many :companies, through: :yard_companies end class School < ActiveRecord::Base belongs_to :company belongs_to :student belongs_to :cinema, counter_cache: true end class Teacher < ActiveRecord::Base belongs_to :movie, counter_cache: true belongs_to :company end class Contract < ActiveRecord::Base belongs_to :company belongs_to :student end class Company < ActiveRecord::Base has_many :teachers has_many :movies, through: :teachers has_many :contracts has_many :students, through: :contracts end
If I write this in my movie_controller.rb :
@students = @movie.cinema.companies.students.all
I have this error:
undefined method 'students' for #Company :: ActiveRecord_Associations_CollectionProxy: 0x00000007f13d88>
If instead I write this:
@students = @movie.cinema.companies.find(6).students.all
it shows me the correct students in my select_collection.
How to better understand this process?
UPDATE
I need to collect_select each student in the movie theater companies of this movie.
How to write?
source share