Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

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?