mfaerevaag
mfaerevaag

Reputation: 730

as_json not including association

I am trying to override the as_json method in my Edge class so I can include the x and y coordinates of it's from and to Vertex.

The assosiations are like this:

Edge belongs_to :from and :to Vertex.

Vertex has_many :edges, where :x and :y are fields.

I have tried several syntactic variation, like the one below, but can't get it to work. Thanks for any help!

def as_json(options={})
  super only: [:name, :value, :color], include: [
                                  { from: { only: [:x, :y] } },
                                  {   to: { only: [:x, :y] } } 
end

Upvotes: 1

Views: 112

Answers (1)

Lazarus Lazaridis
Lazarus Lazaridis

Reputation: 6029

I think you should use the following:

super only: [:name, :value, :color],
        include: {
          from: { only: [:x, :y] },
          to: { only: [:x, :y] }
        }

Upvotes: 1

Related Questions