Pan Wangperawong
Pan Wangperawong

Reputation: 1250

How to fetch Paperclip photo urls in a SQL Query - Rails 3.1, Paperclip

I am trying to write an API for an app using rails as the backend, which shows meal listing. Each meal item shows a name and photo. For the API, I am trying to return a JSON array, which contains all photo urls associated with a meal model and ordered by highest ranking through a sql query. I am unsure what I am suppose to put in the SELECT statement.

How the model looks:

Meal
  has_many :photos
  #other attributes
end

Photo
  belongs_to :meal
  # paperclip stuff
end

All photos are uploaded through paperclip. The setup is based on Emerson Lackey's tutorial. I am using PostgreSQL as the database.

If there is a better way please also let me know.

Upvotes: 2

Views: 718

Answers (1)

maniek
maniek

Reputation: 7307

something like (untested):

render :json=>@meal.photos.all(:order=>'whatever').map(&:photo_url)

Upvotes: 1

Related Questions