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 に答える