In rails how can I get raw sql results from the database and then turn part of them into active record objects.
More specifically, suppose I have a select statement like: "SELECT comments.*, c1.sort_order, c2.sort_order" and I want to take each row build a Comment object and then run comment.setpath(c1, c2). I'm sure there is a function that builds objects and does all the conversions but search as I might I can't seem to locate it.
Note that there are plenty of questions I've found on this site that explain how I can execute raw sql that returns rows from a model's table and have that automatically turned into act objects or get raw database values. I want to get the raw data, harvest the extra info I need and turn some of my results into activerecord objects.
CodePudding user response:
Are you trying to do something like this:
ActiveRecord::Base.connection.execute("sql here").map do |hash|
new_info = harvest_info(hash)
Comment.new(hash.merge(new_info)) if some_requirement?
end