【发布时间】:2017-11-11 16:43:27
【问题描述】:
我正在尝试使用“person”模型、“event”模型和“event_person”模型创建一个应用程序,以存储哪些人正在参加哪些活动的详细信息。
我已经设置好了,所以每个人都有很多事件,这些事件通过 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
当我尝试在 person 模型中显示一个条目,并在我的 people_controller.rb 文件中突出显示一行时:
def show
@people = Person.find(params[:id])
@events = @people.events
end
它突出显示的行是@events = @people.events 作为问题,但我似乎无法弄清楚我做错了什么。
任何指针都非常感谢。
谢谢
【问题讨论】:
标签: ruby-on-rails model controller