Forum: Ruby on Rails simple select

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Petan C. (Guest)
on 2009-02-27 13:31
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.
Frederick C. (Guest)
on 2009-02-27 13:49
(Received via mailing list)
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
MaD (Guest)
on 2009-02-27 13:51
(Received via mailing list)
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)
MaD (Guest)
on 2009-02-27 13:54
(Received via mailing list)
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.
Petan C. (Guest)
on 2009-02-27 14:47
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.
Eifion Bedford (Guest)
on 2009-02-27 15:38
(Received via mailing list)
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
http://asciicasts.com
Twitter: @eifion




On Fri, Feb 27, 2009 at 12:47 PM, Petan C. <
Petan C. (Guest)
on 2009-02-27 16:05
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.
MaD (Guest)
on 2009-02-27 17:08
(Received via mailing list)
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>
This topic is locked and can not be replied to.