你如何在铁轨中模仿“喜欢”?

我有3个模型:用户,对象,喜欢

目前,我有模型:用户有很多对象。 我如何进行建模:

1)用户可以喜欢很多物体

2)一个对象可以有很多喜欢(来自不同的用户)

所以我希望能够做到这样的事情:

User.likes =用户喜欢的对象列表

Objects.liked_by =对象喜欢的用户列表

下面的模型肯定是错的……

class User  :likes end class Likes < ActiveRecord::Base belongs_to :user belongs_to :object end class Objects  :likes end 

为了进一步阐述我对Brandon Tilley的回答,我建议如下:

 class User < ActiveRecord::Base # your original association has_many :things # the like associations has_many :likes has_many :liked_things, :through => :likes, :source => :thing end class Like < ActiveRecord::Base belongs_to :user belongs_to :thing end class Thing < ActiveRecord::Base # your original association belongs_to :user # the like associations has_many :likes has_many :liking_users, :through => :likes, :source => :user end 

你很亲密; 使用a :through ,relation,首先必须建立你正在经历的关系:

 class User < ActiveRecord::Base has_many :likes has_many :objects, :through => :likes end class Likes < ActiveRecord::Base belongs_to :user belongs_to :object end class Objects < ActiveRecord::Base has_many :likes has_many :users, :through => :likes end 

请注意, Objects应该has_many :likes ,以便外键位于正确的位置。 (另外,你应该为你的模型使用单数formsLikeObject 。)

这是实现这一目标的简单方法。 基本上,只要使用:class_name选项指定正确的类名,就可以根据需要创建任意数量的关系。 但是,它并不总是一个好主意,因此请确保在任何给定请求期间只使用一个,以避免其他查询。

 class User < ActiveRecord::Base has_many :likes, :include => :obj has_many :objs has_many :liked, :through => :likes, :class_name => 'Obj' end class Like < ActiveRecord::Base belongs_to :user belongs_to :obj end class Obj < ActiveRecord::Base belongs_to :user has_many :likes, :include => :user has_many :users, :through => :likes # having both belongs to and has many for users may be confusing # so it's better to use a different name has_many :liked_by, :through => :likes, :class_name => 'User' end u = User.find(1) u.objs # all objects created by u u.liked # all objects liked by u u.likes # all likes u.likes.collect(&:obj) # all objects liked by u o = Obj.find(1) o.user # creator o.users # users who liked o o.liked_by # users who liked o. same as o.users o.likes # all likes for o o.likes.collect(&:user) 

根据轨道建模的命名约定的模型和关联

 class User < ActiveRecord::Base has_many :likes has_many :objects, :through => :likes end class Like < ActiveRecord::Base belongs_to :user belongs_to :object end class Object < ActiveRecord::Base belongs_to :user has_many :likes has_many :users, :through => :likes end 

此外,你可以使用已经内置的gem,如act-as-taggable-on ,可以在没有代码的情况下拥有相同的function:)