ValeriiVasin
ValeriiVasin

Reputation: 8706

Rails activerecord select fields AS

Help me please. How to do rails select() with AS option?

I can do

Model.select([:photo_medium_rec, :profile_id])

but this data is extracting for JSON and it will be convenient to work with fields 'photo' and 'profile'. How can I do this?

PS. Only for one field I can do

Model.select("photo_medium_rec AS photo")

How about multiple?

Upvotes: 0

Views: 759

Answers (2)

Arif Widianto
Arif Widianto

Reputation: 1

If you really want a solution with select, I believe you can do that with:

Model.select("photo_medium_rec AS photo, profile_id as profile")

Upvotes: 0

Leonid Shevtsov
Leonid Shevtsov

Reputation: 14179

I would override the Model#to_json method with

def to_json
  JSON.dump({:photo => photo_medium_rec, :profile => profile_id})
end

instead of messing with select.

Upvotes: 1

Related Questions