postgresql Rails“ActiveRecord_Associations_CollectionProxy 的未定义方法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31409792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:58:21  来源:igfitidea点击:

Rails "undefined method for ActiveRecord_Associations_CollectionProxy"

ruby-on-railsrubypostgresqlruby-on-rails-4activerecord

提问by

I have this 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 movies_controller.rb:

如果我在我的movies_controller.rb 中写这个:

@students = @movie.cinema.companies.students.all

@students = @movie.cinema.companies.students.all

I have this error:

我有这个错误:

undefined method 'students' for #Company::ActiveRecord_Associations_CollectionProxy:0x00000007f13d88>

#Company::ActiveRecord_Associations_CollectionProxy:0x00000007f13d88> 的未定义方法“学生”

If instead I write this:

如果我写这个:

@students = @movie.cinema.companies.find(6).students.all

@students = @movie.cinema.companies.find(6).students.all

it shows me the correct students in my select_collection.

它向我显示了我的 select_collection 中正确的学生。

How to better understand this process?

如何更好地理解这个过程?

UPDATE:

更新

I need the collection_select of every students in companies of a cinema of this movie.

我需要这部电影的电影院公司的每个学生的collection_select。

How to write?

怎么写?

采纳答案by Matt

As described by Nerminyou're trying to request a collection of children, from a collection of children.

正如Nermin所描述的,您试图从一组儿童中请求一组儿童。

You could use collectto gather the students from the companies along the lines of:

您可以使用以下collect方式从公司收集学生:

@movie.cinema.companies.collect(&:students).flatten.uniq

But I think you would do better to add a scope to your Student model along the lines of:

但我认为您最好按照以下方式为您的学生模型添加一个范围:

scope :for_companies, ->(_companies) {joins(:companies).where(company: _companies)}

Called with Student.for_companies(@movie.cinema.companies)

Student.for_companies(@movie.cinema.companies)

Disclaimer: untested, but should be a starting point!

免责声明:未经测试,但应该是一个起点!

回答by Nermin

@students = @movie.cinema.companies.students.allto explain why this throws error

@students = @movie.cinema.companies.students.all解释为什么这会引发错误

@movie.cinemawill give you cinema of the movie

@movie.cinema会给你电影的电影院

@movie.cinema.companieswill give you a list of companies for that cinema as an ActiveRecord_Association_CollectionProxy

@movie.cinema.companies将为您提供该电影院的公司列表作为 ActiveRecord_Association_CollectionProxy

Then when you call studentson the CollectionProxyof companies via @movie.cinema.companies.studentsit throws an error because CollectionProxyhas no such method.

然后当你调用studentsCollectionProxy通过的公司@movie.cinema.companies.students,它抛出一个错误,因为CollectionProxy没有这样的方法。

@students = @movie.cinema.companies.find(6).students.allwill work because you get a list of companies, then from that list you find a single company with id 6 and list all students for that single company.

@students = @movie.cinema.companies.find(6).students.all会起作用,因为您获得了公司列表,然后从该列表中找到 ID 为 6 的单个公司并列出该单个公司的所有学生。