Hello,

I'm running into a little hiccup with the json_serializer plug-in. If I try 
to include an association that is an array, the to_json call doesn't 
process the items in the array, just the array itself. I added the 
activesupport fixes you mention in the documentation, but I'm still having 
this issue.

Something like this should be able to reproduce it:

class User < Sequel::Model(DB[:users])
  one_to_many :addresses
end

class Address < Sequel::Model(DB[:addresses])
end

User.first.to_json(
    only: [:first_name, :last_name],
    include: {
      addresses: { only: [:line_1, :line_2, :city, :state, :zip_code] } 
    }
  )
#=> {first_name: 'John', last_name: 'Doe', addresses: [{address_id: 1, 
address_type_id: 1, line_1: 'XXX' line_2: nil, city 'XXX', state: 'XXX', 
zip_code: '12345', created_on: 2016-05-01T01:30:00, updated_on: nil, 
deleted_on: nil}]}


I dove into it a little and I found that InstanceMethods#to_json performs a 
check to see if the included association is an Array. It seems this isn't 
catching for me, and it's processing the array object as a whole.

Here's the section: [direct 
link](https://github.com/jeremyevans/sequel/blob/master/lib/sequel/plugins/json_serializer.rb#L303)

cols.each{|c| h[c.to_s] = get_column_value(c)}
if inc = opts[:include]
  if inc.is_a?(Hash)
    inc.each do |k, v|
    v = v.empty? ? [] : [v]
    h[k.to_s]  = case objs = send(k)
      when 'Array'
        objs.map{|obj| Literal.new(Sequel.object_to_json(obj, *v))}
      else
        Literal.new(Sequel.object_to_json(objs, *v))
      end
    end
  else
    Array(inc).each{|c| h[c.to_s] = send(c)}
  end
end

I was able to have it properly render the objects contained in the array 
with this. (Pardon the dirty hack, I was just testing it out)

cols.each{|c| h[c.to_s] = get_column_value(c)}
if inc = opts[:include]
  if inc.is_a?(Hash)
    inc.each do |k, v|
    v = v.empty? ? [] : [v]
    objs = send(k)
    h[k.to_s] = case objs.class.to_s
      when 'Array'
        objs.map{|obj| Literal.new(Sequel.object_to_json(obj, *v))}
      else
        Literal.new(Sequel.object_to_json(objs, *v))
      end
    end
  else
    Array(inc).each{|c| h[c.to_s] = send(c)}
  end
end

Let me know if I might be doing something wrong here.
Thank you.



-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to