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

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.