ruby on rails - Using Ransacker to filter through relations -


i have following model entities in rails application:

class artist < activerecord::base   has_many :albums end  class album < activerecord::base   belongs_to :artist   has_many :tracks end  class track < activerecord::base   belongs_to :album end 

i'm trying implement search form within index view of album entity need select albums artist.name starts search parameter. i've got success dealing direct model searches situation, having verify parent attribute select children elements, it's giving headaches.

i've read ransacker documentation don't find related :(

to solve in general way, created file _search.html.slim, containing "general search logic", allowing both "simple" searches (without associations), , complex searches (with associations):

= search_form_for @search, url: request.original_url |f|   = f.condition_fields |c|     .field       = c.attribute_fields |a|         - if local_assigns.has_key? :associations           = a.attribute_select associations: associations         - else           = a.attribute_select       = c.predicate_select compounds: false, only: [:cont, :not_cont, :eq, :lteq, :gteq, :lt, :gt]       = c.value_fields |v|         = v.text_field :value   .form-actions     = f.submit 'search', class: 'btn btn-primary' 

which use in index files as

= render 'search', associations: [:artist] 

passing associations want search on, or

= render 'search' 

in case of no associations

my controller looks like

def index   @search = @albums.includes(:artist).search(params[:q])   @albums = @search.result(distinct: true).paginate(per_page: 10, page: params[:page])   @search.build_condition end 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -