# Simple select

**URL:** <https://www.ruby-forum.com/t/simple-select/161561>\
**Category:** Rails\
**Created:** [February 27, 2009, 12:31pm UTC](https://www.ruby-forum.com/t/simple-select/161561 "2009-02-27T12:31:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [February 27, 2009, 12:31pm UTC](https://www.ruby-forum.com/t/simple-select/161561/1 "2009-02-27T12:31:09Z")

</div>

Hi all,

I have two models gallery and photo (has\_many -\> belongs\_to).  
Can I use this to find the primary photo for the given gallery?

@gallery = @user.galleries  
@primary\_photo = @gallery.photos.find\_by\_primary(true).first

it ends with error.

Thanks for any help.  
P.

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [February 27, 2009, 12:49pm UTC](https://www.ruby-forum.com/t/simple-select/161561/2 "2009-02-27T12:49:33Z")

</div>

On 27 Feb 2009, at 11:31, Petan C. wrote:

> Hi all,
> 
> I have two models gallery and photo (has\_many -\> belongs\_to).  
> Can I use this to find the primary photo for the given gallery?
> 
> @gallery = @user.galleries  
> @primary\_photo = @gallery.photos.find\_by\_primary(true).first

find\_by\_xxx returns a single object (or nil) so that call to first is  
superfluous.

Fred

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [February 27, 2009, 12:51pm UTC](https://www.ruby-forum.com/t/simple-select/161561/3 "2009-02-27T12:51:54Z")

</div>

the correct call is:  
@primary\_photo = @gallery.photos.find(:first, :conditions =\> {:primary  
=\> true})

or (but thats an old one and i don’t know if that isn’t deprecated):  
@primary\_photo = @gallery.photos.find\_first\_by\_primary(true)

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [February 27, 2009, 12:54pm UTC](https://www.ruby-forum.com/t/simple-select/161561/4 "2009-02-27T12:54:53Z")

</div>

or you could add a named\_scope to your photo-model:  
named\_scope :primary, :conditions =\> { :primary =\> true }

then your call could be like:  
@primary\_photo = @gallery.photos.primary.first

this should be the nicest alternative.

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [February 27, 2009, 1:47pm UTC](https://www.ruby-forum.com/t/simple-select/161561/5 "2009-02-27T13:47:40Z")

</div>

Thx for all your suggestions, but it always ended up with this error.

NoMethodError in GalleriesController#index  
undefined method `photos’ for #Class:0x54a56dc

def index  
@title = “gallery preview”  
@user = User.find(params[:user\_id])  
@gallery = @user.galleries  
@primary = @gallery.photos.primary.first  
end

I think, that my routes should be fine.

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [February 27, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/simple-select/161561/6 "2009-02-27T14:38:27Z")

</div>

It looks from your code as if you’re returning a list of user galleries  
rather than a single one, so the list you get won’t have a photos  
method.  
Does your code work if you change the third line in your method to

@gallery = @user.galleries.first

Eifion

> **[ASCIIcasts - Recent Episodes](https://asciicasts.com)**
>
> A variety of short articles about Ruby and Rails based on Ryan Bates’ Railcasts.

Twitter: @eifion

On Fri, Feb 27, 2009 at 12:47 PM, Petan C. \<

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [February 27, 2009, 4:08pm UTC](https://www.ruby-forum.com/t/simple-select/161561/7 "2009-02-27T16:08:33Z")

</div>

well if you want all the primary photos just use my suggested  
named\_scope. then you can ask:  
@all\_primary\_photos = Photo.all.primary # or just Photo.primary  
@all\_galleries = Gallery.all  
no loop needed. but if you want to loop, you can do that with  
@all\_galleries.each do |gallery|  
# gallery.photos  
end

On 27 Feb., 15:05, Petan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![petan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c57346/32.png) [@petan](https://www.ruby-forum.com/u/petan)\
**Post date:** [February 27, 2009, 3:05pm UTC](https://www.ruby-forum.com/t/simple-select/161561/8 "2009-02-27T15:05:41Z")

</div>

Eifion Bedford wrote:

> It looks from your code as if you’re returning a list of user galleries  
> rather than a single one, so the list you get won’t have a photos  
> method.  
> Does your code work if you change the third line in your method to
> 
> @gallery = @user.galleries.first

Yes, thats the error.  
Now it is working but just for the first one.

Is there a way to loop all of the users galleries?  
Thank you.

P.
