ruby on rails - How do I create a scope to find all objects in my class that `include?` a string on an attribute? -
i have node object has attribute called cached_user_tag_list, stores comma separated list of email addresses string this:
[3] pry(main)> n.cached_user_tag_list => "gerry@test.com, danny@test.com" if set local variable 1 of email addresses, can use ruby's include? string method find if node contains email address. here illustration:
[6] pry(main)> g = "gerry@test.com" => "gerry@test.com" [8] pry(main)> n.cached_user_tag_list.include? g => true what do, create scope on node class/model accept g , search against column/attribute cached_user_tag_list nodes in db , return collection or ar relation of of nodes contain email address stored in variable g within string @ cached_user_tag_list.
how do this?
edit 1
i tried node.where('cached_user_tag_list.include?'(g)), didn't work.
if it's plain old string column comma separated values, can this:
scope :find_nodes_which_include, ->(email) { |email| where("cache_user_tag_list ?", "%#{email}%") }
Comments
Post a Comment