2017-11-11 10 views
1

「人物」モデルと「イベント」モデルと「event_person」モデルを使用して、人々が参加している詳細を保存するアプリケーションを作成しようとしていますどのイベント。Ruby on Railsで 'スルー'モデルを設定する方法

私はこれを設定して、event_personモデルに関連する各人物のイベントが多数あるようにしました。しかし、私はアプリケーションを実行するとエラーが発生していると私は間違って何を理解することはできません。

人モデル:

class Person < ActiveRecord::Base 
belongs_to :team 
has_many :events, through: :event_people 
validates :first_name, presence: true, length: { maximum: 255 } 
validates :last_name, presence: true, length: { maximum: 255 } 
validates :email, presence: true, length: { maximum: 255 } 
scope :ards, ->{ where("team_id = ?",2)} 
end 

イベントモデル:

class Event < ApplicationRecord 
belongs_to :people 
validates :name, presence: true 
end 

Event_personモデル:

class EventPerson < Event 
belongs_to :people 
belongs_to :events 
#accepts_nested_attributes_for :events, :people 
validates :role, presence: true, length: { maximum: 20 } 
end 

私が手にエラーが

Could not find the association :event_people in model Person 
です

私がしようとする人物モデルのエントリを示し、そして私のpeople_controller.rbファイル内の行をハイライトする:

def show 
    @people = Person.find(params[:id]) 
    @events = @people.events 
end 

それが問題として@events = @people.eventsで強調してラインが、私はように見えることはできませんとして私が間違ったことを見つけ出す

大変ありがとうございます。

おかげ

答えて

2

あなたはPersonhas_many :event_peopleを逃している:私は、ApplicationRecordからEventをしませ継承するEventPersonを期待

class EventPerson < Event 
    belongs_to :people 
    belongs_to :events 
    ... 
end 

class Person < ActiveRecord::Base 
    ... 
    has_many :event_people 
    has_many :events, through: :event_people 
    ... 
end 

また、これはすべてのアップマングドようです。また、peopleeventsは?

class EventPerson < ApplicationRecord 
    belongs_to :person 
    belongs_to :event 
    ... 
end 

私は本当にあなたがここに、peopleでやろうとしているのか分からない:

class Event < ApplicationRecord 
    belongs_to :people 
    ... 
end 

おそらく、あなたが実際に意味:

class Event < ApplicationRecord 
    has_many :event_people 
    has_many :people, through: :event_people 
    ... 
end 

はまた、それは少し奇妙ですつまり、@people = Person.find(params[:id])ここに:

def show 
    @people = Person.find(params[:id]) 
    @events = @people.events 
end 

Person.find(params[:id])はレコードのコレクションではなく、単一のレコードを返すためです。私は見たいと思う:

def show 
    @person = Person.find(params[:id]) 
    @events = @person.events 
end 
+0

ありがとうございます。このMVC/ruby​​ on Railsのすべての新人ですので、命名規則の特異性/複数を把握するのに苦労しています。私はあなたが提案した変更を加えて、新しい興味深いエラーを取得しました: 'Mysql2 :: Error:Unknown列 'where clause':SELECT' events'に 'event_people_events.person_id'* FROM 'events' INNER JOIN'イベント '' event_people_events' ON 'events'.'id' =' event_people_events'.event_id' WHERE 'event_people_events'.person_id' = 6' –

+0

これは、event_people_eventsというテーブルを探しています。間違っていて、どこかで命名の問題が原因で推測されています。しかしここで? –

+0

私はそれを理解しました: 'class EventPerson

関連する問題