How do you access elements of one resource in another resource?

Let's say I have one resource called Restaurants that has several things a user adds to it. One of those things is a map. When a user signs in, if I want to give them a recommendation of the day with the title of the restaurant and the map displayed somewhere on the page, how would I go about doing that? Let's say that the app is just going to go through the database and choose a place based on it's id number being one higher than it was yesterday, nothing special. I think I would need to create another resource called DailyRecommendation or something. And then, in that controller, it would need to find the proper id so that it could display restuarant.title and restaurant.map, but I'm not sure how you do that. Is that even how you'd go about it? Thanks!

Posted about 5 years ago by Nate
Posted about 5 years ago by Alex Yang

Nate, there are a few different ways to do this, depending on how complex you want to get:
1. The simplest way is to just choose a random ID from the Restaurants database. So if you have IDs from 1 to 100, then just choose a random number and display the Restaurant information on the page. The problem with this method is that you'll get a different Restaurant every time you visit the page, instead of one consistent recommendation per day.

2. If you need to have one recommendation per day, then you'll need to start getting into automated tasks. I haven't done too much of this myself, but I suggest looking at the Whenever gem (https://github.com/javan/whenever). For example, you could set up a DailyRecommendation database, which would really just be a table with a 'date' column and a 'restaurant_id' column. Every 24 hours, you'd add a new entry with the 'restaurant_id' to recommend for that day. You'd use Whenever to automate the creation of these entries. To display the restaurant info, you would look up the current date in the DailyRecommendation database and use its 'restaurant_id' to look up the correct info from the Restaurants database.

0
Posted about 5 years ago by Nate

That second option sounds a lot more like what I'd want to do. It might be a challenge to set it up, but that will be better in the long run. It also just helps to know that doing something like that is called an automated task. I wasn't finding much help (that I could understand) with my other searches.

Thanks a lot!

0