Ruby on rails 防止此Post模型的实例出现两次(Rails)?

Ruby on rails 防止此Post模型的实例出现两次(Rails)?,ruby-on-rails,Ruby On Rails,我有一个后模型: 现在假设有一篇贴子上有标签食品和饮料,用户已经订阅了这两个标签。他将看到两次邮报;它似乎出现了一次,作为一个贴子标记为食品,然后作为一个贴子标记为饮料 有没有办法防止这样的帖子出现两次?添加:uniq=>true作为的参数,在用户模型中有许多: has_many :subscribed_posts, :source => :posts, :through => :subscribed_tags, :uniq => true 网站上的文件说: :uniq 如果

我有一个后模型:

现在假设有一篇贴子上有标签
食品
饮料
,用户已经订阅了这两个标签。他将看到两次邮报;它似乎出现了一次,作为一个贴子标记为
食品
,然后作为一个贴子标记为
饮料


有没有办法防止这样的帖子出现两次?

添加
:uniq=>true
作为
的参数,在
用户
模型中有许多

has_many :subscribed_posts, :source => :posts, :through => :subscribed_tags, :uniq => true
网站上的文件说:

:uniq

如果为true,则将从集合中忽略重复项。有用的 结合:通过

class Tag < ActiveRecord::Base
  has_many :taggings, :dependent => :destroy  
  has_many :posts, :through => :taggings
  has_many :subscriptions
  #has_many :subscribed_users, :source => :user, :through => :subscriptions
end
class User < ActiveRecord::Base
  (Code related to Devise)

  has_many :posts, :dependent => :destroy
  has_many :subscriptions
  has_many :subscribed_tags, :source => :tag, :through => :subscriptions
  has_many :subscribed_posts, :source => :posts, :through => :subscribed_tags

  attr_writer :subscribed_tag_names
  after_save :assign_subscribed_tags

  def subscribed_tag_names
    @subscribed_tag_names || subscribed_tags.map(&:name).join(' ')
  end

  private

  def assign_subscribed_tags
    #self.subscribed_tags = []
    return if @subscribed_tag_names.blank?
    @subscribed_tag_names.split(" ").each do |name|
      subscribed_tag = Tag.find_or_create_by_name(name)
      self.subscribed_tags << subscribed_tag unless subscribed_tags.include?(subscribed_tag)
    end
  end
end
@posts = current_user.subscribed_posts.paginate(:page => params[:page],
                                                :per_page => 5,
                                                :order => params[:order_by])
has_many :subscribed_posts, :source => :posts, :through => :subscribed_tags, :uniq => true