# Easy newbie question: find conditions

**URL:** <https://www.ruby-forum.com/t/easy-newbie-question-find-conditions/145594>\
**Category:** Rails\
**Created:** [August 11, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/easy-newbie-question-find-conditions/145594 "2008-08-11T22:28:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 11, 2008, 10:28pm UTC](https://www.ruby-forum.com/t/easy-newbie-question-find-conditions/145594/1 "2008-08-11T22:28:28Z")

</div>

Hello!

I am trying to run a simple find, and don’t know how to do it. I’m  
essentially creating a glossary, and I want to be able to find all the  
words that begin with each letter.

Seems like I ought to be able to do something like this:

Word.find(:all, :conditions =\> [‘spelling=?’, ‘a\*’])

But that doesn’t work… How do a find all the records where the  
spelling begins with ‘a’?

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 11, 2008, 10:51pm UTC](https://www.ruby-forum.com/t/easy-newbie-question-find-conditions/145594/2 "2008-08-11T22:51:18Z")

</div>

Excellent, thank you!

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 11, 2008, 10:44pm UTC](https://www.ruby-forum.com/t/easy-newbie-question-find-conditions/145594/3 "2008-08-11T22:44:27Z")

</div>

On Aug 11, 3:28 pm, Morgan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## But that doesn’t work… How do a find all the records where the spelling begins with ‘a’?

You’ll need a LIKE clause:

Work.find :all, :conditions =\> [‘spelling LIKE ?’, ‘a%’]

Note also that SQL uses % instead of \*.

Jeff

REST with Rails, Oct 4, 2008 in Austin, TX:  
[http://www.purpleworkshops.com/workshops/rest-and-web-services](http://www.purpleworkshops.com/workshops/rest-and-web-services)

[softiesonrails.com](http://softiesonrails.com) - blog  
[purpleworkshops.com](http://purpleworkshops.com) - training  
[pragprog.com/titles/cerailn](http://pragprog.com/titles/cerailn) - Upcoming book, Rails for .NET Developers

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 11, 2008, 10:59pm UTC](https://www.ruby-forum.com/t/easy-newbie-question-find-conditions/145594/4 "2008-08-11T22:59:32Z")

</div>

As a further addendum, what if I wanted to find all words that begin  
with ‘a’ and ‘b’ in one query?

If it helps, I’m using sqlite3.

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 11, 2008, 10:55pm UTC](https://www.ruby-forum.com/t/easy-newbie-question-find-conditions/145594/5 "2008-08-11T22:55:00Z")

</div>

On Mon, 2008-08-11 at 22:28 +0200, Morgan K. wrote:

> But that doesn’t work… How do a find all the records where the  
> spelling begins with ‘a’?

* * *

probably depends upon your sql backend but something like this should  
work…

Word.find(:all, :conditions =\> [“spelling LIKE ?”, “a%”])

Craig

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 11, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/easy-newbie-question-find-conditions/145594/6 "2008-08-11T23:27:47Z")

</div>

On Aug 11, 3:59 pm, Morgan K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> As a further addendum, what if I wanted to find all words that begin  
> with ‘a’ and ‘b’ in one query?
> 
> ## If it helps, I’m using sqlite3.
> 
> Posted viahttp://www.ruby-forum.com/.

Probably something like this:

Work.find :all, :conditions =\> [‘spelling LIKE ? OR spelling LIKE ?’,  
‘a%’, ‘b%’]

My SQL-fu isn’t great so there might be a better way to do it.

Jeff

REST with Rails, Oct 4, 2008 in Austin, TX:  
[http://www.purpleworkshops.com/workshops/rest-and-web-services](http://www.purpleworkshops.com/workshops/rest-and-web-services)

[softiesonrails.com](http://softiesonrails.com) - blog  
[purpleworkshops.com](http://purpleworkshops.com) - training  
[pragprog.com/titles/cerailn](http://pragprog.com/titles/cerailn) - Upcoming book, Rails for .NET Developers

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 12, 2008, 12:20am UTC](https://www.ruby-forum.com/t/easy-newbie-question-find-conditions/145594/7 "2008-08-12T00:20:39Z")

</div>

I, too, am using sqlite3 and this works for me:

> > Video.find(:all, :conditions=\>[‘title LIKE ? OR title LIKE ?’, ‘a%’, ‘b%’])  
> > =\> [#\<Video id: 3, title: “a”, added\_by: nil, created\_at: “2008-08-11  
> > 21:05:32”, updated\_at: “2008-08-11 21:05:32”\>, #\<Video id: 4, title:  
> > “b”, added\_by: nil, created\_at: “2008-08-11 21:05:38”, updated\_at:  
> > “2008-08-11 21:05:38”\>]

Morgan K. wrote:

> > Probably something like this:
> > 
> > Work.find :all, :conditions =\> [‘spelling LIKE ? OR spelling LIKE ?’,  
> > ‘a%’, ‘b%’]
> 
> Nope, that doesn’t seem to do it…

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 12, 2008, 12:24am UTC](https://www.ruby-forum.com/t/easy-newbie-question-find-conditions/145594/8 "2008-08-12T00:24:33Z")

</div>

Lake D. wrote:

> I, too, am using sqlite3 and this works for me:
> 
> > > Video.find(:all, :conditions=\>[‘title LIKE ? OR title LIKE ?’, ‘a%’, ‘b%’])  
> > > =\> [#\<Video id: 3, title: “a”, added\_by: nil, created\_at: “2008-08-11  
> > > 21:05:32”, updated\_at: “2008-08-11 21:05:32”\>, #\<Video id: 4, title:  
> > > “b”, added\_by: nil, created\_at: “2008-08-11 21:05:38”, updated\_at:  
> > > “2008-08-11 21:05:38”\>]

That does the trick, thank you!

---

<div class="post-metadata">

**Author:** ![gwendydd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b5e925/32.png) [@gwendydd](https://www.ruby-forum.com/u/gwendydd)\
**Post date:** [August 11, 2008, 11:46pm UTC](https://www.ruby-forum.com/t/easy-newbie-question-find-conditions/145594/9 "2008-08-11T23:46:15Z")

</div>

> Probably something like this:
> 
> Work.find :all, :conditions =\> [‘spelling LIKE ? OR spelling LIKE ?’,  
> ‘a%’, ‘b%’]

Nope, that doesn’t seem to do it…
