开发者

rails 3 find or create based on multiple fields

开发者 https://www.devze.com 2023-02-05 09:23 出处:网络
I am trying to implement a simple tagging system using a tag virtual attribute on a notes object. a tag contains a label and a user_id. what I would like to do is update the HABTM to relationship to t

I am trying to implement a simple tagging system using a tag virtual attribute on a notes object. a tag contains a label and a user_id. what I would like to do is update the HABTM to relationship to the note based on what is in the tags field. I know about the find_or_create_by_X methods, is there something similar I can do with 2 fields i.e., if the label & user_id are unique, create a new ta开发者_运维百科g, else get a tag where the label and user id match. Also is there a similar way of handling the removal of tags?


You can chain fields together using _and_:

Tag.find_or_create_by_label_and_user_id(label_param, user_id_param)
0

精彩评论

暂无评论...
验证码 换一张
取 消