ruby - Convert executed SQL result to a list of Model object -
i'm wondering possible convert executed query result list of models.
i'm using ruby activerecord , need execute custom sql query join 2 or many tables. code looks below:
connection = activerecord::base.connection sql = "select t1.f1, t2.f2 t1 left join t2 on t1.id = t2.id" @result = connection.execute(sql)
in ruby code, defined models manage executed sql result:
class model property :f1, :f2 end
is there way convert @result list of model object? can deal each item in list following
@result.each |item| puts item.f1 puts item.f2 end
have model inherit activerecord::base
class model < activerecord::base end
then can this
connection = activerecord::base.connection sql = "select t1.f1, t2.f2 t1 left join t2 on t1.id = t2.id" @result = model.find_by_sql(sql) puts @result.f1
Comments
Post a Comment