Reputation: 323
Array is filled from query using arbitrary SQL statement
@myarray = myObject.find_by_sql(sql)
SQL is designed in such way that each myarray
item has same fields as myObject
Model, e.g.
\#{myObject value: 100, day: 2013-06-15}
Ideally, I would like to have array of myObjects
Is it at all possible?
Upvotes: 0
Views: 370
Reputation: 10592
From docs for ActiveRecord::Querying#find_by_sql
:
Executes a custom SQL query against your database and returns all the results. The results will be returned as an array with columns requested encapsulated as attributes of the model you call this method from. If you call Product.find_by_sql then the results will be returned in a Product object with the attributes you specified in the SQL query.
So I don't really understand your question. AR will try to map fields returned from query to object fields.
It is accessible in views if you assign it to object variable.
Records never got saved unless you execute one of the methods that perform save.
EDIT:
Remember that AR will not be able to map resultset to records unless you return id column.
Upvotes: 1