# Using a database record as a Find condition

**URL:** <https://www.ruby-forum.com/t/using-a-database-record-as-a-find-condition/190960>\
**Category:** Rails\
**Created:** [July 1, 2010, 5:28pm UTC](https://www.ruby-forum.com/t/using-a-database-record-as-a-find-condition/190960 "2010-07-01T17:28:32Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [July 1, 2010, 5:28pm UTC](https://www.ruby-forum.com/t/using-a-database-record-as-a-find-condition/190960/1 "2010-07-01T17:28:32Z")

</div>

I have a photos model which has a field for a gallery title, I have a  
gallery controller which currently shows all photos that don’t have an  
empty gallery title field.

I want to have on my gallery page a photo from each gallery that has a  
unique title, which would link to ‘gallery/gallery-title’, a page for  
gallery.  
(ie say I set one gallery title to 2009 and another to 2010 then I’d  
have 2 seperate galleries)

I’ve been reading/searching but I don’t know how to implement this.  
How do I use the gallery titles to find and sort the photos??

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [July 1, 2010, 5:39pm UTC](https://www.ruby-forum.com/t/using-a-database-record-as-a-find-condition/190960/2 "2010-07-01T17:39:20Z")

</div>

the batman wrote:

> I have a photos model which has a field for a gallery title, I have a  
> gallery controller which currently shows all photos that don’t have an  
> empty gallery title field.

This is an unnormalized schema, since the gallery title repeats in each  
record that belongs to the same gallery. Unnormalized schemas are  
harder to maintain and deal with, so what you want to ultimately do is  
create a separate galleries table (and Gallery model), associated by  
means of foreign keys.

> I want to have on my gallery page a photo from each gallery that has a  
> unique title, which would link to ‘gallery/gallery-title’, a page for  
> gallery.  
> (ie say I set one gallery title to 2009 and another to 2010 then I’d  
> have 2 seperate galleries)
> 
> I’ve been reading/searching but I don’t know how to implement this.  
> How do I use the gallery titles to find and sort the photos??

Look at find\_by\_\* and the :order option. This is fairly basic  
ActiveRecord stuff, so I’d recommend reviewing the ActiveRecord::Base  
docs.

## Best,

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