I have a where operation on a model that returns a single object. But I can’t seem to use it in object notation (it appears to return an array with the object at [0]).
store = Store.where("some_id = ?", some_id)
puts store.name # doesn't work
puts store # shows array with the object at [0]
Because sometimes you don’t know how many objects a query should return, so for consistency you always get an array.
To get a single object use
If you are looking for the primary ID of the model, you can also use
which will raise a RecrodNotFound exception (handled by rails as a 404 by default) if it doesn’t find the object.