私は、プロジェクトに応募する際に学生と雇用主が関係を結ぶことができる関係モデルを持っています。モデルは次のとおりです。
class Student < User
has_many :relationships, dependent: :destroy
has_many :employers, through: :relationships
end
class Employer < User
has_many :projects
has_many :relationships, dependent: :destroy
has_many :students, through: :relationships
end
class Relationship < ActiveRecord::Base
has_one :project
belongs_to :employer
belongs_to :student
validates_uniqueness_of :project_id, :scope => [:employer_id, :student_id]
end
class Project < ActiveRecord::Base
belongs_to :employer
end
State は、自動的に「posting」に等しく設定されるプロジェクト テーブルの列です。state == :posting のプロジェクトを持つすべての関係を表示しようとしています。ここに私のビューコードがあります:
<% @relationships.each do |relationship| %>
<% if relationship.project.state == :posting %>
<%= relationship.project.inspect %>
<% end %>
<% end %>
また、このビューのコントローラーには次のものがあります。
@relationships = Relationship.all
ビューを開こうとすると、次のようになります。
PG::UndefinedColumn: ERROR: column projects.relationship_id does not exist
私にとって意味をなさないのは、projects テーブルで relationship_id 列を探していないことです。私のコードは、関係からプロジェクトを見つけてから、状態列を見つける必要があります。私は関係 has_one :project を持っていますが、プロジェクトが存在するために関係を必要としないため、 :projects belongs_to の関係はありません。データベースの関係が正しいと確信しています。ビューコードを修正するにはどうすればよいですか? または、データベースの関係が間違っている場合、何が問題なのですか?
更新 雇用主と学生は両方ともポリモーフィックな関連付けによるユーザーモデルであることを忘れていました。ユーザー モデルには、Student または Employer のいずれかの型の列があります。