Which finder syntax is preferred? Or are they the same?
I'm developing on sqlite and I've posted the result of each
query...they kind of look the same but not really. Would this scale
to say, Mysql or Postgres? Thanks for any advice.
named_scope :disabled, :order => 'name', :conditions => { :disabled_at
=> !nil }
SELECT * FROM "groups" WHERE ("groups"."disabled_at" = 't') ORDER BY
name
or
named_scope :disabled, :order => 'name', :conditions => ['disabled_at
<> ?', nil]
SELECT * FROM "groups" WHERE (disabled_at <> NULL) ORDER BY name
> Which finder syntax is preferred? Or are they the same?
> I'm developing on sqlite and I've posted the result of each
> query...they kind of look the same but not really. Would this scale
> to say, Mysql or Postgres? Thanks for any advice.
> named_scope :disabled, :order => 'name', :conditions => { :disabled_at
> => !nil }
> SELECT * FROM "groups" WHERE ("groups"."disabled_at" = 't') ORDER BY
> name
> or
> named_scope :disabled, :order => 'name', :conditions => ['disabled_at
> <> ?', nil]
> SELECT * FROM "groups" WHERE (disabled_at <> NULL) ORDER BY name
Lee Smith wrote: > Which finder syntax is preferred? Or are they the same?
> I'm developing on sqlite and I've posted the result of each > query...they kind of look the same but not really. Would this scale > to say, Mysql or Postgres? Thanks for any advice.
> named_scope :disabled, :order => 'name', :conditions => { :disabled_at > => !nil } > SELECT * FROM "groups" WHERE ("groups"."disabled_at" = 't') ORDER BY > name
> or
> named_scope :disabled, :order => 'name', :conditions => ['disabled_at > <> ?', nil] > SELECT * FROM "groups" WHERE (disabled_at <> NULL) ORDER BY name
Use the first syntax -- the second one is incorrect. I don't know if this is true in SQLite, but in the other DBs, NULL = NULL returns NULL, so your <> NULL construct will return TRUE in all cases, and so it is pointless. In the first case, Rails automatically generates the proper syntax.
Marnen Laibow-Koser wrote: > Lee Smith wrote: >> Which finder syntax is preferred? Or are they the same?
>> I'm developing on sqlite and I've posted the result of each >> query...they kind of look the same but not really. Would this scale >> to say, Mysql or Postgres? Thanks for any advice.
>> named_scope :disabled, :order => 'name', :conditions => { :disabled_at >> => !nil } >> SELECT * FROM "groups" WHERE ("groups"."disabled_at" = 't') ORDER BY >> name
>> or
>> named_scope :disabled, :order => 'name', :conditions => ['disabled_at >> <> ?', nil] >> SELECT * FROM "groups" WHERE (disabled_at <> NULL) ORDER BY name
> Use the first syntax -- the second one is incorrect. I don't know if > this is true in SQLite, but in the other DBs, NULL = NULL returns NULL, > so your <> NULL construct will return TRUE in all cases, and so it is > pointless. In the first case, Rails automatically generates the proper > syntax.
On second thought, *both* are incorrect. Use :conditions => 'disabled_at is not null'.
On Nov 5, 1:19 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas-
s.net> wrote:
> > named_scope :disabled, :order => 'name', :conditions => ['disabled_at
> > <> ?', nil]
> > SELECT * FROM "groups" WHERE (disabled_at <> NULL) ORDER BY name
> Use the first syntax -- the second one is incorrect. I don't know if
> this is true in SQLite, but in the other DBs, NULL = NULL returns NULL,
> so your <> NULL construct will return TRUE in all cases, and so it is
> pointless. In the first case, Rails automatically generates the proper
> syntax.
Actually NULL <> NULL is also NULL. Hooray for 3 way logic. (doesn't
mean the OP's would work though). In general I tend to use the hash
form of conditions when possible, but that's not always the case.