在RABL中显示parent.name而不是parent.id

时间:2014-08-30 11:55:11

标签: ruby-on-rails-4 rabl

我有一个RABL文件:

collection :@points
attributes :id, :point_type, :name, :coordinates
child :point_pages do
  attributes :point_page_type
  child :point_page_classes do
    attributes :auto_class_id, :show_price   # I need to change this line
  end
end

所以,point_page_class belongs_to :point_pagebelongs_to :auto_class

我需要在attributes的{​​{1}}中显示point_page_class id,而不是auto_classname。但是,当然,在DB的point_page_classes表中我只有auto_class_id

我需要类似的东西:

child :point_page_classes do
  attributes AutoClass.find(auto_class_id).name, :show_price
end

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

glue方法做到了:

collection :@points
attributes :id, :point_type, :name, :coordinates
child :point_pages do
  attributes :point_page_type
  child :point_page_classes do
    attributes :show_price
    glue(:auto_class) do
      attributes :name
    end
  end
end