DavyJonesLocker / postgres_ext-serializers

MIT License
324 stars 30 forks source link

Sideloading belongs_to produces invalid sql #20

Closed mcm-ham closed 9 years ago

mcm-ham commented 9 years ago
class Tag < ActiveRecord::Base
  belongs_to :note
end

class TagSerializer < ActiveModel::Serializer
  attributes :id
  embed :ids, include: true
  has_one :note
end

class Note < ActiveRecord::Base
  has_many :tags
end

class NoteSerializer < ActiveModel::Serializer
  attributes :id, :content, :name
  has_many   :tags
  embed      :ids, include: true
end

If we serialize tag it produces:

WITH notes_ids AS 
( 
   SELECT "notes"."id" 
   FROM   "notes" 
   WHERE  "notes"."note_id" IN  ( SELECT "tags_ids"."id" FROM   "tags_ids")
)
...

Where tag_ids is defined afterwards so is not found in this query and note_id does not belong in notes table.

danmcclain commented 9 years ago

Fixed by #23