ruby on rails - Modifying the returned value of find_by_sql -


so pulling hair on issue / gotcha. used find_by_sql fetch data database. did because query has lots of columns , table joins , think using activerecord , associations slow down.

i managed pull data , wanted modify returned values. did looping through result ,for example.

a = project.find_by_sql("select mycolumn, mycolumn2 my_table").each |project| project['mycolumn'] = project['mycolumn'].split('_').first end 

what found out project['mycolumn'] not changed @ all.

so question:

does find_by_sql return array hashes? possible modify value of 1 of attributes of hash stated above?

here code : http://pastie.org/4213454 . if can have @ summarize_roles2() that's action taking place.

thank you. im using rails 2.1.1 , ruby 1.8. can't upgrade because of legacy codes.

just change method above access values, print value of project , can check object property.

the results returned array columns requested encapsulated attributes of model call method from.if call product.find_by_sql results returned in product object attributes specified in sql query. if call complicated sql query spans multiple tables columns specified select attributes of model, whether or not columns of corresponding table. 
post.find_by_sql "select p.title, c.author posts p, comments c p.id = c.post_id" > [#<post:0x36bff9c @attributes={"title"=>"ruby meetup", "first_name"=>"quentin"}>, ...] 

source: http://api.rubyonrails.org/v2.3.8/


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -