exists instead of inner join?

22 views
Skip to first unread message

Brad Langhorst

unread,
Apr 10, 2012, 11:41:00 AM4/10/12
to declarative_...@googlegroups.com
I need to set permission on an object based on whether a there exists an object in a has many relationship with a specific property value.

    has_permission_on :conditions, :to => [:new, :create,:update,:read,:show,:index] do
      if_permitted_to :read, :experimental_conditions
      #if_attribute :experimental_conditions =>{ :well_id => is { nil }}
    end

    has_permission_on :experimental_conditions, :to => [:new, :create,:update,:read, :show, :index] do
      if_attribute :well_id => is { nil }
    end

Condition.with_permissions_to(:read).all sort of works... but it creates this sql.
SELECT "conditions".* FROM "conditions" INNER JOIN "experimental_conditions" ON "experimental_conditions"."condition_id" = "conditions".id WHERE (("experimental_conditions"."well_id" IS NULL)) 

This would be better, since it would not return a bunch of un-needed data and would be faster.
SELECT "conditions".* FROM "conditions" where exists (select 1 from  "experimental_conditions" WHERE  "experimental_conditions"."condition_id" = "conditions".id AND (("experimental_conditions"."well_id" IS NULL)) )

I don't see how to do this with d-a. Is it possible?
Is it worth extending rebuild_condition_options to do this?
I don't see immediately how to do it, but i think it's possible.

Thanks!

Brad 
Reply all
Reply to author
Forward
0 new messages