Ho questo modelli:Rails "metodo non definito per ActiveRecord_Associations_CollectionProxy"
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
Se scrivo questo nel mio movies_controller.rb:
@students = @movie.cinema.companies.students.all
ho questo errore:
metodo non definito "studenti" per #Company :: ActiveRec ord_Associations_CollectionProxy: 0x00000007f13d88>
Se invece scrivo questo:
@students = @movie.cinema.companies.find(6).students.all
mi mostra gli studenti corretti nella mia select_collection.
Come comprendere meglio questo processo?
UPDATE:
ho bisogno del collection_select di ogni studente in società di un cinema di questo film.
Come scrivere?
Ci sto provando, @Matt. Grazie per la tua preziosa risposta. Ma nella collection_select per la prima soluzione come recuperare la lista? Sto usando questo: '<% = f.collection_select (: student_id, @students,: id,: name_with_surname, options = {: include_blank => true,: prompt => true})%>' –
@JohnSam che sembra bene, ma come stai riempiendo la variabile '@ studenti'? – Matt
Con questo: '@students = @ movie.cinema.companies.collect (&: students)' in 'movies_controller' sotto' def new'. Come riempire la collezione con la soluzione del metodo '.collect' che mi spieghi? –