我正在尝试在RoR程序中建立多对多关联,但是我无法摆脱标题中提到的错误,即我在Rails控制台是:
User.find(1).attended_events = [Event.find(1)]
我需要解决此问题,在任何地方都找不到,感谢您的帮助。
我的代码:
模型
class User < ApplicationRecord
has_many :created_events,class_name:"Event"
has_many :hosts
has_many :attended_events, through: :hosts
end
class Host < ApplicationRecord
belongs_to :attended_event, class_name:"Event"
belongs_to :attendee, class_name:"User"
end
class Event < ApplicationRecord
belongs_to :creator, foreign_key: :user_id, class_name:'User'
has_many :hosts
has_many :attendees, through: :hosts
end
迁移文件
class CreateUsers < ActiveRecord::Migration[6.0]
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps
end
end
end
class CreateHosts < ActiveRecord::Migration[6.0]
def change
create_table :hosts do |t|
t.references :attendee, null: false, foreign_key: true
t.references :attended_event, null: false, foreign_key: true
t.integer :user_id
t.integer :event_id
t.timestamps
end
end
end
class CreateEvents < ActiveRecord::Migration[6.0]
def change
create_table :events do |t|
t.string :date
t.text :description
t.references :user, null: false, foreign_key: true
t.timestamps
end
end
end
答案 0 :(得分:0)
了解has_many
的{{3}}。
class Event < ApplicationRecord
belongs_to :creator, foreign_key: :user_id, class_name:'User'
has_many :hosts
has_many :attendees, through: :hosts
end
这些模型关联是错误的,错误在第二行。
class Event < ApplicationRecord
belongs_to :creator, foreign_key: :user_id, class_name:'User'
has_many :hosts
has_many :attendees, through: :hosts
end
使用这些模型关联,您需要为foreign_key
语句指定has_many :hosts
。
class Host < ApplicationRecord
belongs_to :attended_event, class_name:"Event"
belongs_to :attendee, class_name:"User"
end
这是我的建议:
用户
class User < ApplicationRecord
has_many :created_events, foreign_key: 'user_id'
has_many :hosts, foreign_key: 'attendee_id'
has_many :attended_events, through: :hosts
end
主机
class Host < ApplicationRecord
belongs_to :attended_event, class_name: 'Event'
belongs_to :attendee, class_name: 'User'
end
事件
class Event < ApplicationRecord
belongs_to :creator, class_name: 'User'
has_many :hosts, foreign_key: 'attended_event_id'
has_many :attendees, through: :hosts
end
您的主机迁移
最好避免在此处传递foreign_key
,它将在数据库中查找attendees
表和attended_events
表。
class CreateHosts < ActiveRecord::Migration[6.0]
def change
create_table :hosts do |t|
# t.references :attendee, null: false, foreign_key: true
t.references :attendee, null: false
# t.references :attended_event, null: false, foreign_key: true
t.references :attended_event, null: false
t.timestamps
end
end
end
答案 1 :(得分:0)
通过关联定义source
class User < ApplicationRecord
has_many :created_events, class_name:"Event"
has_many :hosts
has_many :attended_events, through: :hosts, source: :event
end
class Host < ApplicationRecord
belongs_to :event
belongs_to :user
end
class Event < ApplicationRecord
belongs_to :creator, foreign_key: :user_id, class_name:'User'
has_many :hosts
has_many :attendees, through: :hosts, source: :user
end
class CreateHosts < ActiveRecord::Migration[6.0]
def change
create_table :hosts do |t|
t.references :user, null: false, foreign_key: true
t.references :event, null: false, foreign_key: true
t.timestamps
end
end
end
那么你可以做
User.find(1).attended_events << Event.find(1)