[Rails] Multiple Databases using an abstract class - help
I am trying to figure out how to use an abstract class to access
multiple databases. I can connect just fine using
establish_connections in an abstract class, and I can query the
database using find_by_sql on the abstract class. I am having problems
with then using the data returned from the query. I am looking on any
help on best practices and such.
Here's the setup (simplified for the post)
Computers.rb
self.abstract_class=true
establish_connection :computer_availability (defined in database.yml)
computer_controller.rb
def index
db= Computer.connection
@computers = Computer.find_by_sql(SELECT * FROM compstatus)
--> Returns an array of Computers
end
index.html.erb
Now I want to loop through @computers and show the data from the
query. Since I have a Computer shouldn't I be able to interact with it
like any Rails model:
@computers.each do |computer|
computer.name
end
Nope. This give me this error:
ActionView::TemplateError (Mysql::Error: Table
'computer_availability.computers' doesn't exist: SHOW FIELDS FROM
`computers`)
and in the logs the problem comes form a no_method error on .name
Tried:
@computers.each do |computer|
computer[:name] || computer.index_at(0) and various others
end
Nope. Same Error. no-method error on [ ]
So what can I do with @computers? How do I get at the data? What am I
missing? Where should I kill the connection?
FYI: If I change find_by_sql with connection.execute I can access the
data by doing row[0]
Thanks in advance.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home