首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails用户has_many贴子,has_many favorite_posts

Rails用户has_many贴子,has_many favorite_posts
EN

Stack Overflow用户
提问于 2018-03-15 12:23:29
回答 2查看 802关注 0票数 2

首先,为英语感到抱歉

所以我已经有了“用户-帖子”一对多的关联,这意味着每个帖子只能有一个作者,现在我想在用户配置文件中添加“最喜欢的帖子”按钮,在每个帖子中添加“添加到最喜欢的”按钮,那么问题是如何实现这种正确的方式?我应该重新设计我的用户帖子协会吗?或者创造另一个模型?我有点糊涂。提前感谢!

,实际上,我想要这个结果:

@user.posts #返回此用户创建的所有帖子

@user.favorite_posts #返回此用户添加到收藏夹中的帖子

这是我的用户模型:

代码语言:javascript
复制
class User < ApplicationRecord

  mount_uploader :avatar, ImageUploader
  validates :username, presence: true, uniqueness: true, length: {in: 3..20}

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :validatable

  has_many :posts, dependent: :destroy
  has_many :comments, dependent: :destroy
  has_many :ratings
  enum role: [ :user, :admin ]

  def calculate_average
    ratings.blank? ? 0 : ratings.map(&:value).inject(:+) / ratings.count.to_f
  end
end

职位模式:

代码语言:javascript
复制
class Post < ApplicationRecord
  mount_uploader :image, ImageUploader
  validates :body, presence: true
  validates :title, presence: true, length: { maximum: 50}

  belongs_to :user
  has_many :comments, dependent: :destroy
end

编辑

好吧,看看我是怎么做到的,它的工作方式和我想要的完全一样。

这里是我的用户模型:

代码语言:javascript
复制
class User < ApplicationRecord

  mount_uploader :avatar, ImageUploader
  validates :username, presence: true, uniqueness: true, length: {in: 3..20}

  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :validatable

  has_many :posts, dependent: :destroy
  has_many :comments, dependent: :destroy
  has_many :ratings
  has_many :favorites, dependent: :destroy
  has_many :favorite_posts, through: :favorites, source: "post"
  enum role: [ :user, :admin ]

  def calculate_average
    ratings.blank? ? 0 : ratings.map(&:value).inject(:+) / ratings.count.to_f
  end
end
EN

回答 2

Stack Overflow用户

发布于 2018-03-15 12:52:38

为“UserFavoritePost”存储的post_id和user_id创建一个新模型。并为favorite_posts创建自定义关联。

代码语言:javascript
复制
class UserFavoritePost < ApplicationRecord
  belongs_to :post    
  belongs_to :user
end

class User < ApplicationRecord
  has_many :posts, dependent: :destroy
  has_many :user_favorite_posts
  has_many :favorite_posts, throught: :user_favorite_posts, class: 'Post'

end
票数 1
EN

Stack Overflow用户

发布于 2018-03-16 16:47:08

傻瓜-dev的答案不提供对最喜欢的帖子的直接访问,因此索引视图需要一个循环。普拉萨纳的方法解决了这个问题,但他的回答被不公平地指责为不完整和剽窃。以下是完整的方法:

您需要一个多到多个关系,这是事实,所以您需要一个连接模型和表。但是这个模型是辅助的。不应该有任何重要的逻辑,我不认为它应该有一个控制器或观点。

代码语言:javascript
复制
class User < ApplicationRecord
  has_many :posts, dependent: :destroy # Posts created by this user
  has_many :favs, dependent: :destroy  
  has_many :fav_posts, through: :favs # Favorite posts for this user
end

class Post < ApplicationRecord
  belongs_to :user
  has_many :favs, dependent: :destroy
  has_many :fav_users, through: :favs # Users who have this post as favorite
end

class Fav < ApplicationRecord
  belongs_to :user
  belongs_to :post
end

这允许使用用户类中的两种不同方法访问用户创建的所有帖子和他喜欢的所有帖子。

代码语言:javascript
复制
@posts = current_user.posts         # Posts created by this user
@fav_posts = current_user.fav_posts # Favorite posts 

认为:

代码语言:javascript
复制
<h1><% current_user.name %></h1>
<h2>Your posts</h2>
<%= render @posts %>
<h2>Your favorite posts from other users</h2>
<%= render @fav_posts %>

您不需要一个控制器来创建、查看或删除喜爱的帖子。只需在用户或Post控制器中处理此逻辑即可。例如,对于最喜欢或不喜欢的帖子,只需在PostsController中添加fav和unfav方法即可。

代码语言:javascript
复制
def fav
  current_user.fav_posts << Post.find(params[:id])
end

def unfav
  current_user.favs_posts.destroy(Post.find(params[:id]))
end

认为:

代码语言:javascript
复制
<%= link_to "Favorite", fav_post_path(id: post.id) %>
<%= link_to "Unfavorite", unfav_post_path(id: post.id) %>

您应该在路由中添加以下方法:

代码语言:javascript
复制
post '/posts/:id/fav', to: 'posts#fav', as: 'fav_post'    
post '/posts/:id/unfav', to: 'posts#unfav', as: 'unfav_post'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49299411

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档