has_and_belongs_to_many, avoiding dupes in the join table

前端 未结 12 795
南旧
南旧 2020-12-04 11:07

I have a pretty simple HABTM set of models

class Tag < ActiveRecord::Base 
   has_and_belongs_to_many :posts
end 

class Post < ActiveRecord::Base 
           


        
相关标签:
12条回答
  • 2020-12-04 11:35

    Prevent duplicates in the view only (Lazy solution)

    The following does not prevent writing duplicate relationships to the database, it only ensures find methods ignore duplicates.

    In Rails 5:

    has_and_belongs_to_many :tags, -> { distinct }
    

    Note: Relation#uniq was depreciated in Rails 5 (commit)

    In Rails 4

    has_and_belongs_to_many :tags, -> { uniq }
    

    Prevent duplicate data from being saved (best solution)

    Option 1: Prevent duplicates from the controller:

    post.tags << tag unless post.tags.include?(tag)
    

    However, multiple users could attempt post.tags.include?(tag) at the same time, thus this is subject to race conditions. This is discussed here.

    For robustness you can also add this to the Post model (post.rb)

    def tag=(tag)
      tags << tag unless tags.include?(tag)
    end
    

    Option 2: Create a unique index

    The most foolproof way of preventing duplicates is to have duplicate constraints at the database layer. This can be achieved by adding a unique index on the table itself.

    rails g migration add_index_to_posts
    # migration file
    add_index :posts_tags, [:post_id, :tag_id], :unique => true
    add_index :posts_tags, :tag_id
    

    Once you have the unique index, attempting to add a duplicate record will raise an ActiveRecord::RecordNotUnique error. Handling this is out of the scope of this question. View this SO question.

    rescue_from ActiveRecord::RecordNotUnique, :with => :some_method
    
    0 讨论(0)
  • 2020-12-04 11:41

    In addition the suggestions above:

    1. add :uniq to the has_and_belongs_to_many association
    2. adding unique index on the join table

    I would do an explicit check to determine if the relationship already exists. For instance:

    post = Post.find(1)
    tag = Tag.find(2)
    post.tags << tag unless post.tags.include?(tag)
    
    0 讨论(0)
  • 2020-12-04 11:41

    You can pass the :uniq option as described in the documentation. Also note that the :uniq options doesn't prevent the creation of duplicate relationships, it only ensures accessor/find methods will select them once.

    If you want to prevent duplicates in the association table you should create an unique index and handle the exception. Also validates_uniqueness_of doesn't work as expected because you can fall into the case a second request is writing to the database between the time the first request checks for duplicates and writes into the database.

    0 讨论(0)
  • 2020-12-04 11:43

    Set the uniq option:

    class Tag < ActiveRecord::Base 
       has_and_belongs_to_many :posts , :uniq => true
    end 
    
    class Post < ActiveRecord::Base 
       has_and_belongs_to_many :tags , :uniq => true
    
    0 讨论(0)
  • 2020-12-04 11:46

    To me work

    1. adding unique index on the join table
    2. override << method in the relation

      has_and_belongs_to_many :groups do
        def << (group)
          group -= self if group.respond_to?(:to_a)
          super group unless include?(group)
        end
      end
      
    0 讨论(0)
  • 2020-12-04 11:46

    Extract the tag name for security. Check whether or not the tag exists in your tags table, then create it if it doesn't:

    name = params[:tag][:name]
    @new_tag = Tag.where(name: name).first_or_create
    

    Then check whether it exists within this specific collection, and push it if it doesn't:

    @taggable.tags << @new_tag unless @taggable.tags.exists?(@new_tag)
    
    0 讨论(0)
提交回复
热议问题