ruby - Rails "undefined method for ActiveRecord_Associations_CollectionProxy" -
i have 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 write in movies_controller.rb:
@students = @movie.cinema.companies.students.all
i have error:
undefined method 'students' #company::activerecord_associations_collectionproxy:0x00000007f13d88>
if instead write this:
@students = @movie.cinema.companies.find(6).students.all
it shows me correct students in select_collection.
how better understand process?
update:
i need collection_select of every students in companies of cinema of movie.
how write?
as described nermin you're trying request collection of children, collection of children.
you use collect
gather students companies along lines of:
@movie.cinema.companies.collect(&:students).flatten.uniq
but think better add scope student model along lines of:
scope :for_companies, ->(_companies) {joins(:companies).where(company: _companies)}
called student.for_companies(@movie.cinema.companies)
disclaimer: untested, should starting point!
Comments
Post a Comment