Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


darius goore
darius goore
3,803 Points

Display 2 random items from Postgres DB

I am building a test app to help me learn RoR.

In the app, I want to display 2 random images from the db.

First question is, where do I put this method? in the model or the controller? (right now it's in the Model)

here is the method: def makematch @objects = Object.find(:all, :limit => 2, :order => "RANDOM()") end

I think this will find 2 records and I can create two hashes to pull the appropriate avatar_urls

second question is once I have the method, how do I access it in the view?

Totally confused in this.

1 Answer

darius goore
darius goore
3,803 Points

I moved the logic to the view which basically works:

The initial assignment returns #<ActiveRecord::Relation::ActiveRecord_Relation_Object:0x00000101f97908> on screen above the images. How can I get this logic to work from the model?