ruby-on-railsneo4j.rb

Neo4jrb: a way to query for the model_class while doing collection_select


Is there a way to have a couple generic lines of code that can return results from the correct model_class based on the the model definition, and not on the lines of code?

Right now, I'm using a generator to create:

<%= f.collection_select(:semanticZoomOut, Class.all.sort { |a,b| a.name <=> b.name },
  :id, :name, options = {
    :prompt => "Please Select an Item",
    :selected => @instance.semanticZoomOut.map(&:id)
  }, html_options = {:multiple => true, :class=>"search"}) %>

Where "Class" has to be manually changed for each _form.html.erb. Preferably, I'd like to generate something like this:

<%= f.collection_select(:semanticZoomOut, @instance.semanticZoomOut.class.all.sort { |a,b| a.name <=> b.name },
  :id, :name, options = {
    :prompt => "Please Select an Item",
    :selected => @instance.semanticZoomOut.pluck(id)
  }, 
  html_options = {:multiple => true, :class=>"search"}) %>

Solution

  • How about this?

    clazz = @instance.class.associations[:semanticZoomOut].model_class.to_s.constantize
    clazz.all.sort_by(&:name)
    

    Or if you want to let Cypher do the work:

    clazz.order(:name)