设置rails模型关联

时间:2012-05-15 19:50:30

标签: ruby-on-rails database ruby-on-rails-3 activerecord associations

我的rails应用程序的用户模型与兴趣和假期有HABTM关系。这两个(兴趣和假期)只有一个需要编辑并与用户相关的属性。这是设置和工作正常。

我需要创建一个名为friend_birthday的新模型,其中包含有关用户朋友的生日(以及他们各自的兴趣)的信息。此模型(friend_birthday)需要具有多个属性(:name,:gender,:dob和:interests)。

我正在考虑为此使用has_many / belongs_to。用户有很多friends_birthdays和friends_birthdays属于用户。

这听起来不错吗?我怎么能实现这个?

感谢!!!!

1 个答案:

答案 0 :(得分:1)

是的,这听起来不错,但我认为将模特朋友命名为

会更好
class Friend
  belongs_to :user
  #also u can use HABTM for interests, 
  #but it is better to use rich join table and polymorphic association
  has_and_belongs_to_many :interests 
end

class User
  has_many :friends
end

Ofc如果朋友不是用户))只是RL朋友

相关问题