ruby on rails - Convert PG::Result to an Active Record model -


pg-ruby allows send multiple queries database in 1 shot, helps minimize number of trips made database:

results = [] conn.send_query('query1;query2;query3') conn.block while result = conn.get_result   results << result end 

given result know active record model, what's appropriate way convert result models? right i'm doing following:

fields = result.fields models = result.values.map { |value_set|    model.new(hash[fields.zip(value_set)]) } 

the problem method each of active record objects don't appear #persisted? since they've been instantiated .new.

i think want activerecord::base.instantiate method. correctly handle new_record?/persisted? issue, finding correct class single-table inheritance.

for example:

fields = result.fields models = result.values.map { |value_set|    model.instantiate(hash[fields.zip(value_set)]) } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -