11

私は3つのモデルを持っています:ユーザー、オブジェクト、いいね

現在、私はモデルを持っています:ユーザーは多くのオブジェクトを持っています。モデリングを行うにはどうすればよいですか?

1)ユーザーは多くのオブジェクトを好きになることができます

2)オブジェクトは(さまざまなユーザーからの)多くのいいねを持つことができます

だから私はこのようなことをしたいと思っています:

User.likes=ユーザーが高く評価したオブジェクトのリスト

Objects.liked_by=オブジェクトが高く評価したユーザーのリスト

以下のモデルは間違いなく間違っています...

class User < ActiveRecord::Base
  has_many :objects
  has_many :objects, :through => :likes
end

class Likes < ActiveRecord::Base
  belongs_to :user
  belongs_to :object
end

class Objects < ActiveRecord::Base
  belongs_to :users
  has_many :users, :through => :likes    
end
4

4 に答える 4

18

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
于 2012-08-14T07:21:20.270 に答える
4

あなたは近くにいます。リレーションを使用する:throughには、最初に、実行するリレーションを設定する必要があります。

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(また、おそらく単数形LikeObjectモデルに使用する必要があります。)

于 2012-08-14T03:46:04.933 に答える
1

これを実現する簡単な方法を次に示します。基本的に、:class_nameオプションを使用して適切なクラス名を指定する限り、必要な数の関係を作成できます。ただし、常に良いアイデアとは限らないため、追加のクエリを回避するために、特定のリクエストでは1つだけを使用するようにしてください。

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)
于 2012-08-14T06:34:16.943 に答える
0

レールモデリングの命名規則に従ったモデルと関連付け

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

また、acts-as-taggable-onのようなすでに組み込まれているgemを使用して、コードなしで同じ機能を使用できます:)

于 2012-08-14T07:24:46.107 に答える