YuKagi
YuKagi

Reputation: 2501

Rails nested resources available in another context

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...

Upvotes: 1

Views: 117

Answers (1)

Gareth
Gareth

Reputation: 138042

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

Upvotes: 1

Related Questions