views:

87

answers:

3

I intend to store in a database a minimal amount of information pertaining to a book (title, isbn). However, on display I'd like to display additional attributes (pages, author, cover image) not stored in the database. I plan on getting this information from the Amazon Web Services (AWS) using Ruby/AWS. I am very new to Ruby on Rails and am not sure exactly how this would/should be performed.

Some options that spring to mind include modifying the books controller or creating an entirely separate model based on the Amazon entities that relies upon searching for books listed only in my database.

+3  A: 

Your ActiveRecord book model can be customized to retrieve the additional attributes from AWS. Your view and controllers will be clueless and DRY.

jrhicks
That makes good sense. Should I create definitions or attributes to do the lookup from the model?
ahsteele
Create methods named (pages, author, cover_image, etc). In these methods fetch the amazon information and return the value asked. You will probably wont want to make an AWS call for each attribute so cache the AWS results by tucking it into an instance variable of the active record such as (@my_aws_result). That won't persist, but at least you will limit the AWS requests to 1 per end user request. Obviously you can employ more advanced caching without any worry of disrupting your controller and views. The model will encapsulate those details.
jrhicks
+1  A: 

I would suggest creating methods in the model. Are you always going to be pulling the extra info every time you show these entries? If not you will want to maybe have a different controller action for either the "extended" (ie with amazon info) or "limited" (ie without the amazon info) and have the other be the standard. This way you can pull exactly what you want when you want it and make sure you hit amazon only when you need to, not with every single show or index call.

Shane Liebling
+3  A: 

When you ever need to deal with any data, in any MVC environment, it needs to go in the model. You could create an additional *other_information* method that uses ||= to either get the other information from AWS or returns the information from an instance variable. Either that or you could create a *find_with_info* method that gets it all at the same time :)

But defiantly keep any data-related tasks in the model.

Jamie Rumbelow