开发者

Why is Rail2's to_json's include option returning empty hashes?

开发者 https://www.devze.com 2023-03-31 07:55 出处:网络
I\'m trying to use Rails(2)\'s to_json model serializing mechanism to emit some data from associated models.As my guide I\'m referencing the following essentially identical documentation URLs:

I'm trying to use Rails(2)'s to_json model serializing mechanism to emit some data from associated models. As my guide I'm referencing the following essentially identical documentation URLs:

  • http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html
  • http://apidock.com/rails/ActiveRecord/Serialization/to_json

Here is some of the relevant model code:

class WorkEffortAssignment < ActiveRecord::Base
  belongs_to :work_effort
  belongs_to :assigned_to, :polymorphic => true
  belongs_to :assigned_by, :polymorphic => true

Here is the controller code, I'm just trying to dump some JSON for initial testing purposes:

def dump_work_effort_assignments
    WorkEffort.include_root_in_json = false
    all_assignments = WorkEffortAssignment.all
    options = {:include => [:work_effort, :assigned_to, :assigned_by], :only => [:work_effort_id, :assigned_to_id, :assigned_by_id]}
    ext_json = "{data:#{WorkEffortAssignment.all.to_json(options)}}"
    render :inline => ext_json
end

Here's the first record of Json data with empty hashes for work_effort, assigned_to and assigned_by:

{data:[{"assigned_to":{},"work_effort_id":"9","assigned_to_id":3,"assigned_by":{},"work_effort":{},"a开发者_Go百科ssigned_by_id":3}, //etcetera

But below is my console session showing the associations that I'd like to represent in my Json. So what am I doing wrong in my controller when trying to specify the include option for to_json, such that I can easily send associated model data back to the browser. Thanks in advance

>> assignment = WorkEffortAssignment.first
=> #<WorkEffortAssignment id: 1, assigned_at: nil, assigned_from: nil, assigned_
thru: nil, unassigned_at: nil, assigned_to_id: 3, assigned_to_type: "Party", ass
igned_by_id: 3, assigned_by_type: "Party", created_at: nil, updated_at: nil, wor
k_effort_id: "9">
>> assignment.work_effort
=> #<WorkEffort id: 9, description: "Software Architecture Document", type: nil,
 started_at: nil, finished_at: nil, projected_completion_time: nil, actual_compl
etion_time: nil, created_at: "2011-08-18 13:39:30", updated_at: "2011-08-25 13:3
9:30", facility_id: nil, facility_type: nil, work_effort_record_id: nil, work_ef
fort_record_type: nil, projected_cost_id: nil, actual_cost_id: nil, parent_id: n
il, lft: 1, rgt: 2>
>> assignment.assigned_to
=> #<Party id: 3, description: "George Jempty", business_party_id: 2, business_p
arty_type: "Individual", list_view_image_id: nil, enterprise_identifier: nil, cr
eated_at: "2011-08-29 14:21:41", updated_at: "2011-08-29 14:21:41">
>> assignment.assigned_by
=> #<Party id: 3, description: "George Jempty", business_party_id: 2, business_p
arty_type: "Individual", list_view_image_id: nil, enterprise_identifier: nil, cr
eated_at: "2011-08-29 14:21:41", updated_at: "2011-08-29 14:21:41">


The :only specifier you have there is throwing it off. The following will include the child-objects:

def dump_work_effort_assignments
    WorkEffort.include_root_in_json = false
    all_assignments = WorkEffortAssignment.all
    options = {:include => [:work_effort, :assigned_to, :assigned_by]}
    ext_json = "{data:#{WorkEffortAssignment.all.to_json(options)}}"
    render :inline => ext_json
end

...but will not filter out the attributes of the parent. If you want to do that, it may be simpler to just build a hash and convert it to json:

def dump_work_effort_assignments
    data = WorkEffortAssignment.all.map {|wea| {
        :work_effort => wea.work_effort,
        :assigned_to => wea.assigned_to,
        :assigned_by => wea.assigned_by
    }}
    ext_json = "{data:#{data.to_json}}"
    render :inline => ext_json
end
0

精彩评论

暂无评论...
验证码 换一张
取 消