0

我的控制器中有这个相当长的 json 输出,我希望将它移动到模型中,因为这似乎是一个好习惯。首先,控制器方法:

def dashboard
  @line_items = LineItem.all

  respond_to do |format|
    format.json { render json: @line_items.as_json(include: {project: {include: {spec: {methods: [:dashboard_due_at]} },methods:[:company_name, :contacts_names, :owner_names] }}, methods:[:dashboard_created_at, :dashboard_length])}
  end
end

我怎样才能将所有从 as_json 开始的东西移动到 LineItem 模型中,这样我就可以做到

format.json { render  json: @line_items.dashboard_json }

谢谢你!

4

1 回答 1

1

在您的 LineItem 模型中

def dashboard_json
  as_json(include: {project: {include: {spec: {methods: [:dashboard_due_at]} },
    methods:[:company_name, :contacts_names, :owner_names] }},
    methods:[:dashboard_created_at, :dashboard_length])
end
于 2012-06-23T01:09:48.053 回答