特定の教授が教えるコースのリストを返すrailsクエリを作成しようとしています。私が使用しようとしていますクエリは次のとおりです。多対多と結合を使用するRailsスコープ
def self.taught_by(professor_id)
Course
.joins(:sections)
.joins(:professors)
.where(['profesor_id = ?', professor_id]).select('distinct courses.*')
end
するSQLite3 ::のSQLException:いいえ、そのようなコラム:profesor_id:S ...
どのように私は別のを参照するwhere句を使用することができます表? professor_idはセクションテーブルにあります。
教授:
class Professor < ApplicationRecord
enum status: { sabbatical: 0, available: 1 }
has_many :sections
has_many :courses, :through => :sections
belongs_to :department
validates :name, presence: true
validates :name, length: { in: 4..32 }
validates :name, uniqueness: { case_sensitive: false }
def self.search(term)
if term
where('name LIKE ?', "%#{term}%").order('name DESC')
else
order('name DESC')
end
end
end
章:
class Section < ApplicationRecord
has_many :enrollments
belongs_to :professor
belongs_to :course
validates_uniqueness_of :professor_id, scope: :course_id
scope :by_professor_id, ->(prof_id) { where('professor_id = ?', prof_id) }
end
はコース:
class Course < ApplicationRecord
enum status: { planning: 0, offered: 1 }
scope :offered, -> { where(status: 1) }
scope :planning, -> { where(status: 0) }
belongs_to :department
has_many :sections
has_many :professors, through: :sections
validates :title, :number, :status, :description, presence: true
validates :description, length: { in: 10..500 }
validates :title, :number, uniqueness: { case_sensitive: false }
def self.search(term)
if term
where('title LIKE ?', "%#{term}%").order('title DESC')
else
order('title ASC')
end
end
def self.taught_by(professor_id)
Course
.joins(:sections)
.joins(:professors)
.where(['profesor_id = ?', professor_id]).select('distinct courses.*')
end
end
任意のヘルプは本当にだろう次.rb
ファイルと
感謝。私はこれに固執しています。
初期の思考:あなたは、静的メソッド 'taught_by'でを使用しようとしているように思えます実際には静的ではない( 'self.taught_by'、' taught_by'だけではありません)と定義されている 'Course'です。 –