我有这些模型:
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
如果我在我的 movies_controller.rb 文件中写下以下代码:
@students = @movie.cinema.companies.students.all
我会遇到以下错误信息:
undefined method 'students' for #Company::ActiveRecord_Associations_CollectionProxy:0x00000007f13d88>
如果我改为这样写:
@students = @movie.cinema.companies.find(6).students.all
它就能正确显示我所需的选定公司中的学生。
如何更好地理解这个过程?
更新:
我需要在电影院里对于每个公司的学生进行选集选择。
应该怎么写?
<%= f.collection_select(:student_id, @students, :id, :name_with_surname, options = {:include_blank => true, :prompt => true}) %>
- user4412054@students
变量的呢? - Matt@students = @movie.cinema.companies.collect(&:students)
在movies_controller
的def new
方法中,如何填充集合以解决.collect
方法的问题? - user4412054