# Which finder syntax would be preferred as far as being the most database agnostic?

**URL:** <https://www.ruby-forum.com/t/which-finder-syntax-would-be-preferred-as-far-as-being-the-most-database-agnostic/178474>\
**Category:** Rails\
**Created:** [November 5, 2009, 5:41am UTC](https://www.ruby-forum.com/t/which-finder-syntax-would-be-preferred-as-far-as-being-the-most-database-agnostic/178474 "2009-11-05T05:41:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [November 5, 2009, 5:41am UTC](https://www.ruby-forum.com/t/which-finder-syntax-would-be-preferred-as-far-as-being-the-most-database-agnostic/178474/1 "2009-11-05T05:41:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [November 5, 2009, 6:38am UTC](https://www.ruby-forum.com/t/which-finder-syntax-would-be-preferred-as-far-as-being-the-most-database-agnostic/178474/2 "2009-11-05T06:38:01Z")

</div>

I for one prefer the first syntax, since it eliminates SQL from the  
ruby code, but that’s just my preference.

Cheers,  
Tim

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [November 5, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/which-finder-syntax-would-be-preferred-as-far-as-being-the-most-database-agnostic/178474/3 "2009-11-05T14:19:51Z")

</div>

Lee S. 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.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [November 5, 2009, 2:22pm UTC](https://www.ruby-forum.com/t/which-finder-syntax-would-be-preferred-as-far-as-being-the-most-database-agnostic/178474/4 "2009-11-05T14:22:53Z")

</div>

Marnen Laibow-Koser wrote:

> Lee S. 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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

> ## Best,
> 
> Marnen Laibow-Koser  
> [http://www.marnen.org](http://www.marnen.org)  
> removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [November 5, 2009, 2:37pm UTC](https://www.ruby-forum.com/t/which-finder-syntax-would-be-preferred-as-far-as-being-the-most-database-agnostic/178474/5 "2009-11-05T14:37:59Z")

</div>

On Nov 5, 1:19 pm, Marnen Laibow-Koser \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> 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
