# Completely un-understandable behavior in a simple Model

**URL:** <https://www.ruby-forum.com/t/completely-un-understandable-behavior-in-a-simple-model/111918>\
**Category:** Rails\
**Created:** [August 13, 2007, 1:54pm UTC](https://www.ruby-forum.com/t/completely-un-understandable-behavior-in-a-simple-model/111918 "2007-08-13T13:54:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![KSRajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c77e96/32.png) [@KSRajesh](https://www.ruby-forum.com/u/KSRajesh)\
**Post date:** [August 13, 2007, 1:54pm UTC](https://www.ruby-forum.com/t/completely-un-understandable-behavior-in-a-simple-model/111918/1 "2007-08-13T13:54:47Z")

</div>

Hi,

I have been working on RoR for about 8 months now. I have 3 websites  
live and running well on RoR using all sorts of inter-table  
relationships (has\_many, HABTM, has\_many, :through -= you name it).

It is in this background that the results I am seeing today are taking  
me completely out of my depths.

I have 2 tables

1. tasks
2. opportunities  
and Task and Opportunity corresponding to them as models.

So, I go to  
ruby script/console  
and say  
t = Task.find(:all)  
and then I do  
t[0].class  
and it says Opportunity!!!  
How is this possible? Of course, I did this exercise in script/console  
because none of the relationships that I had mapped into Task were  
getting detected.

For more detailed info, here are the models

class Task \< ActiveRecord::Base

set\_table\_name “tasks”  
validates\_presence\_of :who\_id, :what, :due\_date  
belongs\_to :opportunity, :foreign\_key =\> “ref\_id”  
belongs\_to :who, :foreign\_key =\> “who\_id”, :class\_name =\> “User”  
belongs\_to :creator, :foreign\_key =\> “created\_by”, :class\_name =\>  
“User”

def self.filter\_by\_responsibility(id)  
find(:all, :conditions =\> “who\_id = #{id} and status =  
‘Open’”, :order =\> “type, due\_date”)  
end

def self.filter\_by\_opportunity(opportunity\_id)  
find(:all, :conditions =\> “ref\_id = #{opportunity\_id} and type  
= ‘Opportunity’ and status = ‘Open’”, :order =\> “due\_date asc”)  
end  
end

class Opportunity \< ActiveRecord::Base  
belongs\_to :contact  
belongs\_to :business\_line  
belongs\_to :line\_owner, :class\_name =\> “User”, :foreign\_key =\>  
“line\_owner\_id”  
belongs\_to :cxo\_owner, :class\_name =\> “User”, :foreign\_key =\>  
“cxo\_owner\_id”  
has\_many :tasks  
has\_many :pending\_tasks, :class\_name=\>“Task”, :conditions=\>“status !=  
‘Done’”  
belongs\_to :proposal\_owner, :class\_name =\> “User”, :foreign\_key =\>  
“proposal\_owner\_id”  
belongs\_to :proposal\_currency, :foreign\_key =\>  
“proposal\_currency\_id”, :class\_name =\> “Currency”  
validates\_numericality\_of :proposal\_value, :message =\> “: Enter  
proposal value without commas, decimal points etc”, :if =\> Proc.new { |  
o| !o.proposal\_value.blank? }

def self.overdue\_closures  
find(:all, :conditions =\> “expected\_closure\_date \<= ‘#{Date.today}’  
and current\_status != ‘PO’”, :order =\> “line\_owner\_id”)  
end

end

WHAT IS HAPPENING HERE? WHAT AM I MISSING? IT CANT BE RAILS. IT NEVER  
IS.

Regards,  
Rajesh

---

<div class="post-metadata">

**Author:** ![KSRajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c77e96/32.png) [@KSRajesh](https://www.ruby-forum.com/u/KSRajesh)\
**Post date:** [August 14, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/completely-un-understandable-behavior-in-a-simple-model/111918/2 "2007-08-14T13:41:47Z")

</div>

K. Rajesh wrote:

> t = Task.find(:all)  
> and then I do  
> t[0].class  
> and it says Opportunity!!!

Do you have a db field called ‘type’ containing ‘Opportunity’?

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![KSRajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c77e96/32.png) [@KSRajesh](https://www.ruby-forum.com/u/KSRajesh)\
**Post date:** [August 18, 2007, 9:06am UTC](https://www.ruby-forum.com/t/completely-un-understandable-behavior-in-a-simple-model/111918/3 "2007-08-18T09:06:26Z")

</div>

Mark, I do indeed have a field called type with its value set to  
Opportunity. How does it affect things? I was pleasantly surprised to  
see such an accurate prediction!

Regards,  
Rajesh

---

<div class="post-metadata">

**Author:** ![KSRajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c77e96/32.png) [@KSRajesh](https://www.ruby-forum.com/u/KSRajesh)\
**Post date:** [August 18, 2007, 9:55am UTC](https://www.ruby-forum.com/t/completely-un-understandable-behavior-in-a-simple-model/111918/4 "2007-08-18T09:55:29Z")

</div>

On 8/18/07, K. Rajesh [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Mark, I do indeed have a field called type with its value set to  
> Opportunity. How does it affect things?

Look up “single table inheritance”, or STI, in the API docs/google.

Isak

I was pleasantly surprised to

---

<div class="post-metadata">

**Author:** ![KSRajesh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c77e96/32.png) [@KSRajesh](https://www.ruby-forum.com/u/KSRajesh)\
**Post date:** [August 18, 2007, 9:13am UTC](https://www.ruby-forum.com/t/completely-un-understandable-behavior-in-a-simple-model/111918/5 "2007-08-18T09:13:27Z")

</div>

I changed the field name in the database to task\_type and things  
started working fine. What exactly is happening here? Why does a  
database field name affect the Class type in Rails?

Regards,  
Rajesh
