Rails。单个 table 中相同外键的多个实例
Rails. Multiple instances of same foreign key in a single table
我不确定如何为以下要求建模:
我们向用户发送 3 包不同的啤酒。用户分为 tasting_profiles 4 类。然后,我们为所有用户准备了 4 包不同的啤酒。然后用户对啤酒进行评分,这将定期发生。
注意:我主要担心的是我有一个 table(包)可能有多个相同外键的实例。每包由3种啤酒组成。
目前我有以下机型:
用户/啤酒/个人资料/包装/评级
class User < ActiveRecord::Base
belongs_to :profile
has_many :ratings # A user rates every beer received.
has_many :beers, through: :ratings
end
class Beer < ActiveRecord::Base
has_many :ratings
has_many :users, through: :ratings
has_many :packs
end
class Profile < ActiveRecord::Base
has_many :packs #we setup and send periodic packs for each tasting_profile (Profile)
has_many :users #each tasting profile has many users
has_may :beers #each tasting profile has many possible beers
end
class Pack < ActiveRecord::Base
belongs_to :beer #Not sure
belongs_to :profile
end
class Rating < ActiveRecord::Base
belongs_to :user
belongs_to :beer
end
包型号问题:
我的第一个选择是在 Pack 模型中包含以下字段:
- pack_id
- profile_id (FK)
- beer_id (FK1)
- beer_id (FK2)
- beer_ip (FK3)
- delivery_month
这样我就有了一整套入场了。
在这里进行研究,我读到这显然是一种不好的做法,并且只建议了一个 FK 用于入口。像这样:
- pack_id
- profile_id (FK)
- beer_id (FK)
- delivery_month
在这种情况下,我将为每个包提供 3 个条目。
最后我考虑研究一个数组字段(不确定是否可以用外键完成):
- pack_id
- profile_id (FK)
- 啤酒 [beer_id1 , beer_id2, beer_id3] # 或散列 [beer_1: beer_id, beer_2: ..]
- delivery_month
创建包后,我需要根据用户数量根据需要使用以下条目填充评级 table。如果 tasting_profile 中有 100 个用户,我将向他们发送 3 个啤酒包,从而在此处产生 300 个条目
- rating_id
- User_id (FK)
- beer_id (FK)
- 评分
- delivery_date
我真的很困惑如何正确建模。 ¡我会感谢任何可能的帮助!我试图尽可能详细。如果您需要进一步说明,请告诉我。
¡¡提前致谢!!
花了我很多时间,但比预期的要简单。一旦我在同一个 table 中需要同一个 foreign_id 的多个实例,就应该很清楚我需要另一个连接 table.
首先,我尝试使用 has_and_belongs_to_many 关联,但在删除连接 table 中的 "association" 条目时遇到问题。所以我以 has_many 低谷结束。这些是更改:
class Beer < ActiveRecord::Base
has_many :pack_details
has_many :packs through: :pack_details
...
end
class Pack < ActiveRecord::Base
has_many :pack_details, dependent: destroy
has_many _beers, through: : pack_details
accepts_nested_attributes_for :pack_details, allow_destroy => true
...
end
class PackDetail < ActiveRecord::Base
Belongs_to :pack
Belongs_to :beer
...
end
一切正常!
我不确定如何为以下要求建模:
我们向用户发送 3 包不同的啤酒。用户分为 tasting_profiles 4 类。然后,我们为所有用户准备了 4 包不同的啤酒。然后用户对啤酒进行评分,这将定期发生。
注意:我主要担心的是我有一个 table(包)可能有多个相同外键的实例。每包由3种啤酒组成。
目前我有以下机型:
用户/啤酒/个人资料/包装/评级
class User < ActiveRecord::Base
belongs_to :profile
has_many :ratings # A user rates every beer received.
has_many :beers, through: :ratings
end
class Beer < ActiveRecord::Base
has_many :ratings
has_many :users, through: :ratings
has_many :packs
end
class Profile < ActiveRecord::Base
has_many :packs #we setup and send periodic packs for each tasting_profile (Profile)
has_many :users #each tasting profile has many users
has_may :beers #each tasting profile has many possible beers
end
class Pack < ActiveRecord::Base
belongs_to :beer #Not sure
belongs_to :profile
end
class Rating < ActiveRecord::Base
belongs_to :user
belongs_to :beer
end
包型号问题: 我的第一个选择是在 Pack 模型中包含以下字段:
- pack_id
- profile_id (FK)
- beer_id (FK1)
- beer_id (FK2)
- beer_ip (FK3)
- delivery_month
这样我就有了一整套入场了。
在这里进行研究,我读到这显然是一种不好的做法,并且只建议了一个 FK 用于入口。像这样:
- pack_id
- profile_id (FK)
- beer_id (FK)
- delivery_month
在这种情况下,我将为每个包提供 3 个条目。
最后我考虑研究一个数组字段(不确定是否可以用外键完成):
- pack_id
- profile_id (FK)
- 啤酒 [beer_id1 , beer_id2, beer_id3] # 或散列 [beer_1: beer_id, beer_2: ..]
- delivery_month
创建包后,我需要根据用户数量根据需要使用以下条目填充评级 table。如果 tasting_profile 中有 100 个用户,我将向他们发送 3 个啤酒包,从而在此处产生 300 个条目
- rating_id
- User_id (FK)
- beer_id (FK)
- 评分
- delivery_date
我真的很困惑如何正确建模。 ¡我会感谢任何可能的帮助!我试图尽可能详细。如果您需要进一步说明,请告诉我。
¡¡提前致谢!!
花了我很多时间,但比预期的要简单。一旦我在同一个 table 中需要同一个 foreign_id 的多个实例,就应该很清楚我需要另一个连接 table.
首先,我尝试使用 has_and_belongs_to_many 关联,但在删除连接 table 中的 "association" 条目时遇到问题。所以我以 has_many 低谷结束。这些是更改:
class Beer < ActiveRecord::Base
has_many :pack_details
has_many :packs through: :pack_details
...
end
class Pack < ActiveRecord::Base
has_many :pack_details, dependent: destroy
has_many _beers, through: : pack_details
accepts_nested_attributes_for :pack_details, allow_destroy => true
...
end
class PackDetail < ActiveRecord::Base
Belongs_to :pack
Belongs_to :beer
...
end
一切正常!