Hello all,
I have 2 tables calls and clients:
clients:
Hello all,
I have 2 tables calls and clients:
clients:
Hi,
Following up more on this...
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 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
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