Neo4jrb:一种在执行 collection_select 时查询 model_class 的方法

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

有没有一种方法可以让几行通用代码能够 return 根据模型定义而不是代码行从正确的 model_class 中得到结果?

现在,我正在使用生成器创建:

<%= 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"}) %>

其中 "Class" 必须为每个 _form.html.erb 手动更改。 最好,我想生成这样的东西:

<%= 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"}) %>

这个怎么样?

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

或者,如果您想让 Cypher 完成工作:

clazz.order(:name)