edit 2
If you stumble across this, check both answers as I’d now use pluck for this
I have a fairly large custom dataset that I’d like to return to be echoe’d out as json. One part is:
l=Location.find(row.id)
tmp[row.id]=l
but I’d like to do something like:
l=Location.find(row.id).select("name, website, city")
tmp[row.id]=l
but this doesn’t seem to be working. How would I get this to work?
thx
edit 1
alternatively, is there a way that I can pass an array of only the attributes I want included?
In Rails 2
…or…
This reference doc gives you the entire list of options you can use with
.find, including how to limit by number, id, or any other arbitrary column/constraint.In Rails 3 w/ActiveRecord Query Interface
Ref: Active Record Query Interface
You can also swap the order of these chained calls, doing
.select(...).where(...).first– all these calls do is construct the SQL query and then send it off.