theDrifter theDrifter - 1 year ago 77
JSON Question

ActiveRecord Connection Adapter return value

I had a bit more complex sql-query which i decided to use plain

rather than writing it with AR. I wrapped the sql-statement inside ActiveRecord::Base.connection.exec_query` .

The method basically looks like that:

def sc_agent
return ActiveRecord::Base.connection.exec_query("SQL").as_json

The serializer
gives me a json hash with json strings. So also id's are now strings


On standard queries where i use AR i receive nicely
formatted json with the correct types.

How can i retain the correct types of all the values when using the ConnectionAdapter directly? Thanks for everything!

Answer Source

Use find_by_sql method.

Example: Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date].

More info is here:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download