42

I have a standard many-to-many relationship between users and roles in my Rails app:

class User < ActiveRecord::Base
  has_many :user_roles
  has_many :roles, :through => :user_roles
end

I want to make sure that a user can only be assigned any role once. Any attempt to insert a duplicate should ignore the request, not throw an error or cause validation failure. What I really want to represent is a "set", where inserting an element that already exists in the set has no effect. {1,2,3} U {1} = {1,2,3}, not {1,1,2,3}.

I realize that I can do it like this:

user.roles << role unless user.roles.include?(role)

or by creating a wrapper method (e.g. add_to_roles(role)), but I was hoping for some idiomatic way to make it automatic via the association, so that I can write:

user.roles << role  # automatically checks roles.include?

and it just does the work for me. This way, I don't have to remember to check for dups or to use the custom method. Is there something in the framework I'm missing? I first thought the :uniq option to has_many would do it, but it's basically just "select distinct."

Is there a way to do this declaratively? If not, maybe by using an association extension?

Here's an example of how the default behavior fails:

    >> u = User.create
      User Create (0.6ms)   INSERT INTO "users" ("name") VALUES(NULL)
    => #<User id: 3, name: nil>
    >> u.roles << Role.first
      Role Load (0.5ms)   SELECT * FROM "roles" LIMIT 1
      UserRole Create (0.5ms)   INSERT INTO "user_roles" ("role_id", "user_id") VALUES(1, 3)
      Role Load (0.4ms)   SELECT "roles".* FROM "roles" INNER JOIN "user_roles" ON "roles".id = "user_roles".role_id WHERE (("user_roles".user_id = 3)) 
    => [#<Role id: 1, name: "1">]
    >> u.roles << Role.first
      Role Load (0.4ms)   SELECT * FROM "roles" LIMIT 1
      UserRole Create (0.5ms)   INSERT INTO "user_roles" ("role_id", "user_id") VALUES(1, 3)
    => [#<Role id: 1, name: "1">, #<Role id: 1, name: "1">]
KingPong
  • 1,439
  • 1
  • 16
  • 22

8 Answers8

29

As long as the appended role is an ActiveRecord object, what you are doing:

user.roles << role

Should de-duplicate automatically for :has_many associations.

For has_many :through, try:

class User
  has_many :roles, :through => :user_roles do
    def <<(new_item)
      super( Array(new_item) - proxy_association.owner.roles )
    end
  end
end

if super doesn't work, you may need to set up an alias_method_chain.

austinfromboston
  • 3,791
  • 25
  • 25
  • It doesn't work like that. I'll update the post to include the test. – KingPong Aug 22 '09 at 17:24
  • Thanks, I'll try the association extension. – KingPong Aug 24 '09 at 15:42
  • That worked perfectly. Thanks! The part I was missing when I tried something like this myself was the proxy_owner bit. – KingPong Aug 24 '09 at 18:05
  • 3
    For posterity, the above method can be shortened and genericized to: def <<(*items) super(items - proxy_target) end – KingPong Aug 24 '09 at 20:22
  • That was my first version :). I didn't have actual models set up and was concerned that super(nil) might cause errors. updating the answer with your version and leaving the sub-par code here ( for posterity ): def <<(*items); new_items = items - proxy_owner.roles; super( new_items ) unless new_items.empty?; end – austinfromboston Aug 24 '09 at 22:13
  • 1
    For Rails 3.1, `s/proxy_owner/proxy_association.owner/` [related Q](http://stackoverflow.com/questions/7001810/alternative-method-for-proxy-owner-in-activerecord) – Turadg Dec 04 '12 at 20:00
  • 1
    Why make the argument `*items` when `<<` takes only a single object? http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-3C-3C – Turadg Dec 07 '12 at 20:12
  • It's an easy way to transparently convert the added item to an array. I'll update it with the new idioms. – austinfromboston Dec 17 '12 at 05:07
  • 2
    It's odd to me that << dedupes for `has_many` but not `has_many :through`. However a Rails issue to fix this (https://github.com/rails/rails/issues/8573) was rejected with "This is your domain logic so it your responsibility to check it." – Turadg Dec 20 '12 at 21:51
  • @Turadg Now `<<` can take more than one argument http://guides.rubyonrails.org/association_basics.html#methods-added-by-has-many-collection-object – lulalala Oct 03 '16 at 03:16
29

Use Array's |= Join Method.

You can use Array's |= join method to add an element to the Array, unless it is already present. Just make sure you wrap the element in an Array.

role                  #=> #<Role id: 1, name: "1">

user.roles            #=> []

user.roles |= [role]  #=> [#<Role id: 1, name: "1">]

user.roles |= [role]  #=> [#<Role id: 1, name: "1">]

Can also be used for adding multiple elements that may or may not already be present:

role1                         #=> #<Role id: 1, name: "1">
role2                         #=> #<Role id: 2, name: "2">

user.roles                    #=> [#<Role id: 1, name: "1">]

user.roles |= [role1, role2]  #=> [#<Role id: 1, name: "1">, #<Role id: 2, name: "2">]

user.roles |= [role1, role2]  #=> [#<Role id: 1, name: "1">, #<Role id: 2, name: "2">]

Found this technique on this StackOverflow answer.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
4

You can use a combination of validates_uniqueness_of and overriding << in the main model, though this will also catch any other validation errors in the join model.

validates_uniqueness_of :user_id, :scope => [:role_id]

class User
  has_many :roles, :through => :user_roles do
    def <<(*items)
      super(items) rescue ActiveRecord::RecordInvalid
    end
  end
end
Pete Campbell
  • 301
  • 3
  • 2
  • 2
    Couldn't you change that exception to `ActiveRecord::RecordNotUnique`? I like this answer. Be aware of [race conditions](http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_uniqueness_of) though. – Ashitaka Jan 06 '14 at 02:17
  • Nice answer. I used it without `validates_uniqueness_of`, having declared unique index in database and works charmingly. – Ruby Racer Feb 06 '16 at 14:55
2

i think the proper validation rule is in your users_roles join model:

validates_uniqueness_of :user_id, :scope => [:role_id]
bdon
  • 2,068
  • 17
  • 15
  • Thanks. That doesn't actually do what I want though (which is a set-like behavior), and I've clarified what that is in the original post. Sorry 'bout that. – KingPong Aug 22 '09 at 16:40
  • I think this is the best answer for your problem. If you are careful in creating your interface, a user would have to hack it to add the wrong role anyway, in which case a validation exception is a totally suitable response. – austinfromboston Aug 23 '09 at 23:42
  • 1
    Heh, are you crazy? Users don't add their own roles :-) The typical use case is that a user becomes a member of a role as a side effect of something else. For example, buying a particular product. Other products may also provide the same role, so there is a chance for duplication there. I'd rather do the duplication checking in one place than in whatever random places need to ensure a user has a role. In this sense, giving a user a role he already has is NOT an error condition. – KingPong Aug 24 '09 at 15:40
0

Perhaps it is possible to create the validation rule

validates_uniqueness_of :user_roles

then catch the validation exception and carry on gracefully. However, this feels really hacky and is very inelegant, if even possible.

Schrockwell
  • 838
  • 1
  • 8
  • 25
0

I think you want to do something like:

user.roles.find_or_create_by(role_id: role.id) # saves association to database
user.roles.find_or_initialize_by(role_id: role.id) # builds association to be saved later
Richard Jones
  • 4,760
  • 3
  • 27
  • 34
0

I ran into this today and ended up using #replace, which "will perform a diff and delete/add only records that have changed".

Therefore, you need to pass the union of the existing roles (so they don't get deleted) and your new role(s):

new_roles = [role]
user.roles.replace(user.roles | new_roles)

It's important to note that both this answer and the accepted one are loading the associated roles objects into memory in order to perform the Array diff (-) and union (|). This could lead to performance issues if you're dealing with a large number of associated records.

If that's a concern, you may want to look into options that check for existence via queries first, or use an INSERT ON DUPLICATE KEY UPDATE (mysql) type query for inserting.

0

This will create only one association in the database even if called multiple times Refer rails guide.

user.roles=[Role.first] 
Dyaniyal Wilson
  • 1,012
  • 10
  • 14