ruby-on-rails – Rails“ActiveRecord_Associations_Collection
发布时间:2020-12-16 23:25:56 所属栏目:百科 来源:网络整理
导读:我有这个型号: class Student ActiveRecord::Base has_many :tickets has_many :movies,through: :ticketsendclass Movie ActiveRecord::Base has_many :tickets,dependent: :destroy has_many :students,through: :tickets belongs_to :cinemaendclass Tic
我有这个型号:
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,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 我有这个错误: 未定义的方法’学生’为#Company :: ActiveRecord_Associations_CollectionProxy:0x00000007f13d88> 如果相反我写这个: @students = @ movie.cinema.companies.find(6).students.all 它在我的select_collection中显示了正确的学生. 如何更好地理解这个过程? 更新: 我需要收集这部电影电影公司中每个学生的收藏. 怎么写? 解决方法
如
Nermin所述,您正试图从一系列儿童中请求一组儿童.
您可以使用收集来收集公司的学生: @movie.cinema.companies.collect(&:students).flatten.uniq 但我认为你最好在学生模型中添加一个范围: scope :for_companies,->(_companies) {joins(:companies).where(company: _companies)} 与Student.for_companies(@ movie.cinema.companies)联系 免责声明:未经测试,但应该是一个起点! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- expdp\impdp及exp\imp
- 操作 sqlite数据库 Warning: there is at least one open r
- 在 Swift 中使用 Objective-C 风格的异步 API
- ruby-on-rails – 将设计sign_in形式设计成Twitter Bootstr
- [SQLite][3.6][2-7] SQLite 3 中的数据类型
- 如何让ServiceStack从带有XML命名空间的TFS接收XML请求
- 从Oracle到MySQL,余额宝云实践分享
- c – std ::在std :: string和std :: vector之间移动
- 如何在Ruby中缓存方法结果
- Flash导入矢量图