DavyJonesLocker / postgres_ext-serializers

MIT License
324 stars 30 forks source link

Cancan nested hash condition produces invalid SQL #21

Closed mcm-ham closed 8 years ago

mcm-ham commented 9 years ago

If I serialize this:

Tag.where(note: {name: 'title'})

It produces SQL like this:

SELECT "tags"."id", 
       "tags"."name", 
       "tags"."note_id" 
FROM   "tags" 
WHERE  "note"."name" = 'Title'

Instead of doing a table join on offers.

danmcclain commented 9 years ago

Was this possible without explicitly joining?

mcm-ham commented 9 years ago

Sorry I don't think it was. I think I just got confused with cancancan syntax which allows:

can :index, Tag, note: {name: 'title'}

But after adding postgres_ext-serializers it then generates invalid sql like above.

A workaround people can use is:

class Tag < ActiveRecord::Base
  scope :name_is_title, -> { joins(:note).where(notes: {name:'title'}) }
end

can :index, Tag, Tag.name_is_title do |tag|
  tag.note.title == 'title'
end
felixbuenemann commented 8 years ago

Need to check if this is still an issue. A (skipped) test was added in db578390 (nested filtering support in sideloading_test.rb).

felixbuenemann commented 8 years ago

As far as I can seem this works fine on master, but you have to use where(notes: { name: 'Title' }) (not :note) or it won't work. That's because activerecord does not expand symbols in the were to the table name of the joined relation. It has nothing to do with postgres_ext-serializers.

If this was previously broken, then either #44 or #45 has fixed it.

felixbuenemann commented 8 years ago

Btw. if you don't want to manually specify the correct table name in the where, you can use merge:

Tag.joins(:note).merge(Note.where(name: 'Title'))