I’m having trouble finding resources for my specific problem, or at least I don’t understand the resources I am finding.
I need my Sinatra app to be able to query a remote MySQL db and return some data. I have read-only access to the db so all I need to do is retrieve it. I know the db works, my connection works, and the query I’m trying to use works. Here is some sample code:
db = ActiveRecord::Base.establish_connection(
adapter: 'mysql',
host: host,
database: database,
username: user,
password: pass)
Then I’m trying to query the connection with something like this
@courses = db.find_by_sql("SELECT *")
but the db object doesn’t have the find_by_sql method, or indeed any methods that seem like they’d do the job. Am I missing a step or something to get to where I can query the db or is it something else?
I don’t know what the return value of
establish_connectionis. I suspect it is probably not what you want. The normal way to runfind_by_sqlis:If you have a model already you can also do:
Another way is to directly access the connection adapter object and call
executeon it: