【发布时间】:2021-02-02 10:30:12
【问题描述】:
目标是商店创建奖励并将每个奖励与他选择的追随者相关联。这是我的设置:
class Shop < ApplicationRecord
has_many :rewards
has_many :follows
has_many :users, through: :follows
end
class Reward < ApplicationRecord
belongs_to :shop
end
class Follow < ApplicationRecord
belongs_to :shop
belongs_to :user
has_many :reward_participant
end
class User < ApplicationRecord
has_many :follows
has_many :shops, through: :follows
end
我创建这个模型是为了捕捉奖励和追随者的关联。
class RewardParticipant < ApplicationRecord
belongs_to :reward
belongs_to :follow
end
我已经创建了以下迁移:
class CreateRewards < ActiveRecord::Migration[6.0]
def change
create_table :rewards do |t|
t.string :title
t.text :body
t.date :expires
t.integer :shope_id
t.timestamps
end
end
end
class CreateRewardParticipants < ActiveRecord::Migration[6.0]
def change
create_table :reward_participants do |t|
t.integer :reward_id
t.integer :follow_id
t.timestamps
end
end
end
我无法确定这是否是模型关联和迁移的正确方法。提前感谢您的帮助!
【问题讨论】:
-
Follow over here 是什么?
-
嘿@Gagan Gupta...这是给关注商店的人的!
-
好吧,你的设计还行,但是为什么需要在RewardParticipant中关注。你可以有用户和奖励,因为奖励已经属于特定的商店
-
所以基本上你建议从
Follow和RewardParticipant中删除关注关联,因为我可以使用User和Review关联? -
移除
reward_participants中的关注关联,将follow替换为user,并检查您的所有要求是否得到满足?
标签: ruby-on-rails ruby-on-rails-5