有没有办法让一些通用的代码行可以根据模型定义从正确的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"}) %>
在哪里&#34;班级&#34;必须为每个_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"}) %>
答案 0 :(得分:1)
这个怎么样?
clazz = @instance.class.associations[:semanticZoomOut].model_class.to_s.constantize
clazz.all.sort_by(&:name)
或者如果你想让Cypher完成这项工作:
clazz.order(:name)