Ruby on rails 用户在RubyonRails中标记(喜欢)另一个模型

Ruby on rails 用户在RubyonRails中标记(喜欢)另一个模型,ruby-on-rails,ruby,favorites,Ruby On Rails,Ruby,Favorites,我想在RubyonRails应用程序中实现一个“以后阅读”(就像收藏夹一样)系统。我想要的是用户模型能够标记内容模型以便以后阅读 这两个模型之间的关联如下: class User < ActiveRecord::Base has_many :contents end ------------- class Content < ActiveRecord::Base belongs_to :user end class User < ActiveRecord:

我想在RubyonRails应用程序中实现一个“以后阅读”(就像收藏夹一样)系统。我想要的是
用户
模型能够标记
内容
模型以便以后阅读

这两个模型之间的关联如下:

class User < ActiveRecord::Base
    has_many :contents
end

-------------

class Content < ActiveRecord::Base
    belongs_to :user
end
class User < ActiveRecord::Base
  has_many :contents

  has_many :marked_contents
  has_many :markings, through: :marked_contents, source: :content
end

class MarkedContent < ActiveRecord::Base
  belongs_to :user
  belongs_to :content
end

class Content < ActiveRecord::Base
  belongs_to :user

  has_many :marked_contents
  has_many :marked_by, through: :marked_contents, source: :user
end
class用户
然后一个内容属于
类别
,等等,但这对问题并不重要,所以我没有把它放在那里

用户
可以标记
内容
(可能属于另一个用户),每个用户都会有一个“标记内容(稍后阅读)”列表

我如何实现这一点


我已经读过这个问题,但我并没有真正理解,当试图模拟它时,它不起作用。

你们尝试了什么,什么不起作用

这很直截了当。让我们仔细想想:

有一个用户:

class User < ActiveRecord::Base
end
现在,内容(通常由其他用户创建)可以被其他用户偏爱(标记为“稍后阅读”)。每个用户都可以喜欢(标记“稍后阅读”)任意多的内容,并且每个内容都可以被许多用户喜欢,不是吗?但是,我们必须跟踪哪个用户在某处喜欢哪个内容。最简单的方法是定义另一个模型,比如MarkedContent,来保存这些信息。A has_多:通过关联通常用于建立与另一个模型的多对多连接。因此,相关的关联声明可以如下所示:

class User < ActiveRecord::Base
    has_many :contents
end

-------------

class Content < ActiveRecord::Base
    belongs_to :user
end
class User < ActiveRecord::Base
  has_many :contents

  has_many :marked_contents
  has_many :markings, through: :marked_contents, source: :content
end

class MarkedContent < ActiveRecord::Base
  belongs_to :user
  belongs_to :content
end

class Content < ActiveRecord::Base
  belongs_to :user

  has_many :marked_contents
  has_many :marked_by, through: :marked_contents, source: :user
end
阅读更多内容以了解关联

要将内容标记为收藏夹,一种方法是:

@user = User.first
@content = Content.last

@user.markings << @content 

你尝试了什么,什么不起作用

这很直截了当。让我们仔细想想:

有一个用户:

class User < ActiveRecord::Base
end
现在,内容(通常由其他用户创建)可以被其他用户偏爱(标记为“稍后阅读”)。每个用户都可以喜欢(标记“稍后阅读”)任意多的内容,并且每个内容都可以被许多用户喜欢,不是吗?但是,我们必须跟踪哪个用户在某处喜欢哪个内容。最简单的方法是定义另一个模型,比如MarkedContent,来保存这些信息。A has_多:通过关联通常用于建立与另一个模型的多对多连接。因此,相关的关联声明可以如下所示:

class User < ActiveRecord::Base
    has_many :contents
end

-------------

class Content < ActiveRecord::Base
    belongs_to :user
end
class User < ActiveRecord::Base
  has_many :contents

  has_many :marked_contents
  has_many :markings, through: :marked_contents, source: :content
end

class MarkedContent < ActiveRecord::Base
  belongs_to :user
  belongs_to :content
end

class Content < ActiveRecord::Base
  belongs_to :user

  has_many :marked_contents
  has_many :marked_by, through: :marked_contents, source: :user
end
阅读更多内容以了解关联

要将内容标记为收藏夹,一种方法是:

@user = User.first
@content = Content.last

@user.markings << @content 

你尝试了什么,什么不起作用

这很直截了当。让我们仔细想想:

有一个用户:

class User < ActiveRecord::Base
end
现在,内容(通常由其他用户创建)可以被其他用户偏爱(标记为“稍后阅读”)。每个用户都可以喜欢(标记“稍后阅读”)任意多的内容,并且每个内容都可以被许多用户喜欢,不是吗?但是,我们必须跟踪哪个用户在某处喜欢哪个内容。最简单的方法是定义另一个模型,比如MarkedContent,来保存这些信息。A has_多:通过关联通常用于建立与另一个模型的多对多连接。因此,相关的关联声明可以如下所示:

class User < ActiveRecord::Base
    has_many :contents
end

-------------

class Content < ActiveRecord::Base
    belongs_to :user
end
class User < ActiveRecord::Base
  has_many :contents

  has_many :marked_contents
  has_many :markings, through: :marked_contents, source: :content
end

class MarkedContent < ActiveRecord::Base
  belongs_to :user
  belongs_to :content
end

class Content < ActiveRecord::Base
  belongs_to :user

  has_many :marked_contents
  has_many :marked_by, through: :marked_contents, source: :user
end
阅读更多内容以了解关联

要将内容标记为收藏夹,一种方法是:

@user = User.first
@content = Content.last

@user.markings << @content 

你尝试了什么,什么不起作用

这很直截了当。让我们仔细想想:

有一个用户:

class User < ActiveRecord::Base
end
现在,内容(通常由其他用户创建)可以被其他用户偏爱(标记为“稍后阅读”)。每个用户都可以喜欢(标记“稍后阅读”)任意多的内容,并且每个内容都可以被许多用户喜欢,不是吗?但是,我们必须跟踪哪个用户在某处喜欢哪个内容。最简单的方法是定义另一个模型,比如MarkedContent,来保存这些信息。A has_多:通过关联通常用于建立与另一个模型的多对多连接。因此,相关的关联声明可以如下所示:

class User < ActiveRecord::Base
    has_many :contents
end

-------------

class Content < ActiveRecord::Base
    belongs_to :user
end
class User < ActiveRecord::Base
  has_many :contents

  has_many :marked_contents
  has_many :markings, through: :marked_contents, source: :content
end

class MarkedContent < ActiveRecord::Base
  belongs_to :user
  belongs_to :content
end

class Content < ActiveRecord::Base
  belongs_to :user

  has_many :marked_contents
  has_many :marked_by, through: :marked_contents, source: :user
end
阅读更多内容以了解关联

要将内容标记为收藏夹,一种方法是:

@user = User.first
@content = Content.last

@user.markings << @content 


要使用
有许多:标记
有许多:标记
他需要在关联上指定适当的类名我想我不明白你说的,@Baloo。你能说得更具体些吗?通过使用
has-many:marked-By,through::marked-By contents
rails将尝试查找MarkedBy类,如果你想使用它而不是
,has-many:users,through::marked-By contents
,你将需要使用
has-many:marked-By,class-u-name:'User',通过::marked_contents
有许多相同的东西:markes
根据我的理解,我必须创建一个模型
MarkedContent
,对吗?没有属性?我有点困惑....@gd.silva不,您必须使用user\u id、content\u id作为属性创建MarkedContent,其中每个记录将告诉您,哪些内容(content\u id)由哪个用户(user\u id)标记要使用
有许多:标记
有许多:标记
他需要在关联上指定适当的类名我想我不明白你说的,@Baloo。你能说得更具体些吗?通过使用
has-many:marked-By,through::marked-By contents
rails将尝试查找MarkedBy类,如果你想使用它而不是
,has-many:users,through::marked-By contents
,你将需要使用
has-many:marked-By,class-u-name:'User',通过::marked_contents
有许多相同的东西:markes
根据我的理解,我必须创建一个模型
MarkedContent
,对吗?没有属性?我有点困惑....@gd.silva不,您必须创建带有用户id、内容id作为属性的MarkedContent,其中每个记录将告诉您哪些内容(内容id)由w标记