find_by_sql(["Select * from members, comments where
members.members_id = ? and members.members_id = comments.members_id", accid])
pc.each do |rec|
puts "#{rec.Members_Name} posted #{rec.Comments_Subject}"
end
A few really important things are happening in the preceding example, so we??™ll take
a minute to point out each one:
??? We called the find_by_sql method on the Account model. Really, we could have executed
the same code against any of our defined models though, since we were passing
in a complete SQL string. The find_by_sql method uses only the connection information
of the model it??™s called with.
??? The SQL statements we use with the find_by_sql method must adhere to the specific
SQL syntax that our backend database supports (in this example, we are using SQL Server
so that means we need to use T-SQL syntax). This means, in our example, that we may
need to update our SQL statements if we develop against a SQL Server backend but
later decide to release to production in Oracle, since each implements a different version
of SQL (T-SQL for SQLServer and PL/SQL for Oracle).
Pages:
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400