1

I plan on having galleries with nested images, so images belong in a gallery. I would then like images to have a Boolean option to determine whether or not a particular image shows up on the front page. Setting up a nested resource with a Boolean property is simple, but I haven't yet figured out how to access all images that have that property set to true.

Is iterating through each gallery and image the only way to do that? It seems to me there must be a better way...

user229044
  • 232,980
  • 40
  • 330
  • 338
YuKagi
  • 2,451
  • 3
  • 21
  • 26

1 Answers1

1

Create a scope on the Image class and then you can chain that scope onto any other activerecord list of images:

class Image
  scope :homepage, where(:appear_on_homepage => true)
end

@gallery.images.homepage # Just the relevant images
Gareth
  • 133,157
  • 36
  • 148
  • 157
  • I'll have to try this tonight when I get back to my project. I think I follow it pretty well. :appear_on_homepage is the boolean property, right? Accessing all homepage activated images from all galleries still alludes my understanding. Would it be: (@)galleries = Gallery.all; (@)galleries.images.homepage to access all those images? – YuKagi Sep 07 '11 at 13:45