ruby on rails - Ransacker and Arel on associations fields -
i have project::contribution
belongs_to
user
, , user
has_one
user::profile
.
first_name
, last_name
fields contained in user::profile
i want create ransacker allow me filter contributions user profile first_name
, last_name
.
how can achieved ?
this tried far :
# admin/contribution.rb filter :user_full_name_cont # models/user.rb ransacker :full_name |parent| arel::nodes::infixoperation.new('||', parent.table[:profile_first_name], parent.table[:profile_last_name]) # error end
this fails because of : parent.table[:profile_first_name]
, parent.table[:profile_last_name]
because can't access profile
table so.
finally came solution :
# models/user/profile.rb ransacker :full_name, formatter: proc { |v| v.mb_chars.downcase.to_s } |parent| arel::nodes::namedfunction.new('lower', [arel::nodes::namedfunction.new('concat_ws', [arel::nodes.build_quoted(' '), parent.table[:first_name], parent.table[:last_name]])]) end # admin/user.rb filter :user_profile_full_name_cont
Comments
Post a Comment