Lee Smith
2009-Nov-05 04:39 UTC
Which finder syntax would be preferred as far as being the most database agnostic?
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
Tim Lowrimore
2009-Nov-05 05:36 UTC
Re: Which finder syntax would be preferred as far as being the most database agnostic?
I for one prefer the first syntax, since it eliminates SQL from the ruby code, but that''s just my preference. Cheers, Tim On Nov 4, 10:39 pm, Lee Smith <autige...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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
Marnen Laibow-Koser
2009-Nov-05 13:19 UTC
Re: Which finder syntax would be preferred as far as being t
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 nameUse 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. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Nov-05 13:22 UTC
Re: Which finder syntax would be preferred as far as being t
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''. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org> > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org-- Posted via http://www.ruby-forum.com/.
Frederick Cheung
2009-Nov-05 13:28 UTC
Re: Which finder syntax would be preferred as far as being t
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. Fred> > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.