Using find to get the value from a table referenced by forei

Hello all,

I have 2 tables calls and clients:

clients:
±----------±--------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±----------±--------±-----±----±--------±---------------+
| id | int(11) | | PRI | NULL | auto_increment |
| ivr_id | int(11) | YES | MUL | NULL | |
| is_online | char(1) | YES | | NULL | |
±----------±--------±-----±----±--------±---------------+

calls:
±------------±-------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±------------±-------------±-----±----±--------±---------------+
| id | int(11) | | PRI | NULL | auto_increment |
| src | varchar(80) | YES | | NULL | |
| start | datetime | YES | | NULL | |
| ivr_id | int(11) | YES | MUL | NULL | |
| file_name | varchar(255) | YES | | NULL | |
±------------±-------------±-----±----±--------±---------------+

Here calls.ivr_id is the foreign key referring to clients.ivr_id

Corresponding models are :

class Call < ActiveRecord::Base
belongs_to :client, :foreign_key => “ivr_id”
def self.calls_and_is_online (from_time, to_time)
find (:first,
:conditions => [
“start > :from_time and start <= :to_time
and ivr_id <> 0
and file_name like ‘%wav’”,
{:from_time => from_time, :to_time => to_time}
])
end
end

class Client < ActiveRecord::Base
has_one :call, :foreign_key => “ivr_id”
end

My call_controller has a method

def get_calls_and_is_online
@date = Time.parse(params[:date])
@calls_and_is_online = Call.calls_and_is_online(@date, @date+1.day)
end

From my controller I can get all the details of call table with out
any problems. Now I want to get the is_online status of a call. The
rails book Chap: 18 says I can define some thing like price =
line_item.product.price, but in my case when I use <%=
@calls_and_is_online.client.is_online %> from my view, I get the error

NoMethodError in Call#get_calls_and_is_online

I know that I am missing some thing small, but not able to find what,
even after searching a lot… Any helping hand here is much
appreciated.

raj

Hi,

Following up more on this…

On 11/13/06, Rajkumar S [email protected] wrote:

| is_online | char(1) | YES | | NULL | |
| file_name | varchar(255) | YES | | NULL | |
:conditions => [
end
rails book Chap: 18 says I can define some thing like price =
line_item.product.price, but in my case when I use <%=
@calls_and_is_online.client.is_online %> from my view, I get the error

NoMethodError in Call#get_calls_and_is_online

I added the following in my view:

<%= xx = Call.find (56755) %>
<%= debug(xx.client.class) %>

<%= yy = Client.find(41842) %>
<%= debug (yy.call.class) %>

The corresponding sql queries are:
Call Columns (0.000880) SHOW FIELDS FROM calls
Call Load (0.000436) SELECT * FROM calls WHERE (calls.id = 56755)
LIMIT 1
Client Load (0.000318) SELECT * FROM clients WHERE (clients.id =
17575) LIMIT 1
Client Load (0.000303) SELECT * FROM clients WHERE (clients.id =
41842) LIMIT 1
Client Columns (0.000466) SHOW FIELDS FROM clients
Call Load (0.052974) SELECT * FROM calls WHERE (calls.ivr_id =
41842)

The corresponding values in datbase are:

SELECT ivr_id FROM calls WHERE (calls.id = 56755) LIMIT 1
±-------+
| ivr_id |
±-------+
| 17575 |
±-------+

In the second query, you can see that the value of 17575, is fine, but
it should have queried for clients.ivr_id instead of clients.id.

Now to the third query:

SELECT ivr_id FROM clients WHERE (clients.id = 41842) LIMIT 1;
±-------+
| ivr_id |
±-------+
| 14302 |
±-------+

but the fourth query is SELECT * FROM calls WHERE (calls.ivr_id = 41842)

Here the inverse of the previous problem happened, calls.ivr_id is
correct, but the value is 41842, while it should have been 14302.

I get a feeling that I have messed up the belongs_to and has_many but
I am not able to figure out what it is :frowning:

raj

Hi Rajkumar,

Is there reason you can’t just have a client_id field in the Call and
let Rails to the work ?

I’m assuming the ivr_id field is some kind of “3rd party” information
and it might be better to not use that as the FK relationship between
client and call.

A.

I have finally fixed this:

I was over riding the rails configuration at two places.

  1. Primary key in Clients table is ivr_id instead of id
  2. Foreign key referreng to clients table is ivr_id instead of client_id

So this involves letting rails know that I have overridden the
conventions at 2 places,

  1. in Clients.rb use set_primary_key “ivr_id” so that rails know that
    the primary key is ivr_id instead of id
  2. in Calls.rb use belongs_to :client, :foreign_key => “ivr_id” so
    that rails know that it should use ivr_id instead of client_id

The actuall files are

class Call < ActiveRecord::Base
belongs_to :client, :foreign_key => “ivr_id”

class Client < ActiveRecord::Base
set_primary_key “ivr_id”
has_many :calls

Thanks every one,

raj