0

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

我需要创建一个新的模式称为friend_birthday这就是包含有关的用户朋友(和各自的利益)的生日信息。这个模型(friend_birthday)需要有几个属性(:name,:gender,:dob和:interests)。

我想上使用的has_many/belongs_to的这一个。用户有许多friends_birthdays和friends_birthdays belongs_to用户。

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

谢谢!

+0

你看的has_many:通过? http://guides.rubyonrails.org/association_basics.html – Brett

回答

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朋友

+0

感谢!...什么是接近丰富的连接表的朋友/利益的最佳途径......许多=>透了? – js111

+0

是的,但如果u r不expirienced够了吗,U应使用HABM,这是很容易。 –