Rails 3 find or create based on multiple fields

I am trying to implement a simple tag system using a virtual tag attribute for a notes object. The tag contains the label and user_id. what I would like to do is update the HABTM to the relationship with the note based on what is in the tag field. I know about the methods find_or_create_by_X, is there something similar that I can do with two fields, that is, if the label and user_id are unique, create a new tag, otherwise you will get a tag where the labels and user ID match. Also there is a similar way to handle tag removal?

+5
source share
1 answer

Using _and_can combine fields:

Tag.find_or_create_by_label_and_user_id(label_param, user_id_param)
+11
source

All Articles