2017-04-22 49 views
0

ActiveRecordの周りに私の頭を浮かべて、関連付けを作成するのに本当の問題がありましたので、間違いは簡単です。ActiveRecordクエリのモデルには属性がありません

@user = User.joins(researchers: :students).select(user_attributes).find(current_user.id) 

user_attributes = 'researchers.year, users.givenname, ...電気ショック療法:

は、私はラインを呼び出す @userオブジェクトを持っています。

これは問題なく動作します。私は非常によく似た行を実行しようとすると:

@supervisor = User.joins(researchers: :supervisors).select(supervisor_attributes).where('supervisor.student_id = ?', 1) 

私は@supervisor.surnameを呼び出すしようとすると、supervisor_attributes = 'user.surname'

はノーメソッドのエラーを取得し、私は絶対にないアイデア理由がありません。

私は数や回避策を試してみましたが、私は以下のように私のコードや団体とのより根本的なエラーがあると思う:

student.rb

class Student < ApplicationRecord 
    #Must have the following 
    validates :name, :email, :surname, :email, :supervisor, :registration_number, presence: true 
    #ensures unique email addresses 
    validates :email, uniqueness: true 

    #assosiations 
    belongs_to :researcher 
end 

supervisor.rb

class Supervisor < ApplicationRecord 
    belongs_to :researcher 
end 

researcher.rb

class Researcher < ApplicationRecord 
    #belongs_to :project 
    belongs_to :user 
    has_many :supervisors 
    has_many :students 
end 

user.rb

class User < ApplicationRecord 
    include EpiCas::DeviseHelper 

    has_many :event_registrations 
    has_many :events, through: :event_registrations 
    belongs_to :project 
    has_many :researchers 
    has_many :students, :through => :researchers 
    has_many :supervisors, :through => :researchers 

    # def self.authenticate(username) 
    # where(username: username).first 
    # end 

    end 

すべてのヘルプは非常に高く評価されるだろう!

多くのおかげで、 ジェームズ

+0

エラーメッセージ –

答えて

0

は、複数のではないでしょうか?

supervisor_attributes = 'users.surname' 
+0

を投稿してもいいですが、そうでなければなりませんが、残念ながら同じエラーが発生します。 –

+0

ここで 'supervisors'のところで – Ursus

+1

' @supervisor = User.joins(研究者:監督者).select(supervisor_attributes).where(監督者:{student_id:1})のように書き直すことができます。 – Ursus

0

あなたはそれがsupervisors複数あるべき規則として、databasetable nameを使用する必要がありますwhere clause

supervisor_attributes = 'users.surname' 

@supervisor = User.joins(researchers: :supervisors).select(supervisor_attributes).where('supervisors.student_id = ?', 1) 

次のクエリを試してくださいすることができます。

+0

こんにちはロキバル、私はちょうどそれを試みたと私は同じエラーです。しかし、ありがとう! –

関連する問題