Platinum Partner

Manually Pulling Data From MySQL In Rails

Sometimes you just have to do it yourself, like when you have relationships between different databases.
Here I wanted to retrieve an array of site_ids:


    result = ActiveRecord::Base.connection.execute("SELECT * FROM sites_users WHERE user_id ='#{self.id}'")
    rows = []
    result.each_hash {|h| rows << h['site_id'].to_i }
    return rows


UPDATE: ( An easier, better way)


    rows = ActiveRecord::Base.connection.select_values("SELECT site_id FROM sites_users WHERE user_id ='#{self.id}'")
    return rows.map {|el| el.to_i }


There is also:

select_one(sql, name = nil)
select_value(sql, name = nil)
select_all(sql, name = nil) 
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}