Each account can either have one (or none) coach at any point in time.
I have modeled these relationships in corresponding ActiveRecord classes
as follows:
class Account < ActiveRecord::Base
has_one :coach
end
class Coach < ActiveRecord::Base
belongs_to :account
end
I am trying to use will_paginate for paging through the list of coaches
and look-up their names from the accounts table (and some more fields).
To get to that, I am first trying to create a corresponding find query
in ActiveRecord. Here is my query:
Coach.find :all, :select => 'c.id, a.id, a.name',
:joins => 'as c inner join accounts as a on a.id =
c.account_id',
:order => 'a.name'
It is only returning the following in irb:
[#<Coach id: 99>]
That is, it fails to return the account.id (or a.id) and account.name
(or a.name)
I should mention something else. I am on Rails 2.1.0 and even
find_by_sql does not return columns from multiple tables as the Rails
API documentation says:
This example is from Rails API docs:
# A simple SQL query spanning multiple tables
Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c
WHERE p.id = c.post_id"
> [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup",
"first_name"=>"Quentin"}>, ...]
I followed this for my queries and I can only get columns from either of
the tables but not both.
I should mention something else. I am on Rails 2.1.0 and even
find_by_sql does not return columns from multiple tables as the Rails
API documentation says:
This example is from Rails API docs:
# A simple SQL query spanning multiple tables
Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c
WHERE p.id = c.post_id"
Hello Mark, Priya, and Fred,
I sincerely appreciate you all taking trouble to share your knowledge
with me. I had to be away from development for a day and half to attend
to other urgent matters. I am back on the job and going through your
feedback. I will post the results as soon as I have them.
Regards to you all.
Bharat