On Dec 21, 2007, at 10:02 AM, Frederick Cheung wrote:
>
> exists?
> AR::Base.exists? used to basically say !find(:first, :conditions
> =>)...
>
> It now (effectively) says !find(:first, :select => 'id', :conditions
> => )
>
> I imagine this is to save on instantiating a large object with many
> columns just to throw it away. This is all good and well, but has
> broken some things for us: on some models we have after_find callbacks
> that rely on the presence of attributes. In our particular cases we
> just moved to setting up those things lazily, you could also check
> that the attributes are actually here. However morally I wouldn't
> expect exists? to instantiate anything at all, which would make the
> problem go away (and in a sense take the above change a little
> further).
>
> Rewriting exists? as
>
> def exists?(id_or_conditions)
> sql = construct_finder_sql(:first, :select =>
> "#{quoted_table_name}.#{primary_key}", :conditions =>
> expand_id_conditions(id_or_conditions) :limit =>1)
> connection.select_all(sanitize_sql(sql)).size > 0
> end
>
> does this, but it's rather more verbose, so I'm not sure it's actually
> worth it.
> Thoughts?
>
> Fred
I'm the author of the new exists? code, and I also looked at doing it
with a lower-level connection call instead of a find with :select
option. I don't have the data around anymore, but as I recall, the
benchmarks showed that using the select_all approach didn't perform as
well, so I went with the find with :select approach. I hadn't thought
about the after_find callback interaction, and I guess no one else has
run into that problem before now either. Some stuff has changed in AR
that might have affected performance of the select_all approach since
I tried it last, so it might be worth revisiting now.
--
Josh Susser
http://blog.hasmanythrough.com
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---