Completely un-understandable behavior in a simple Model

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

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'?

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

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

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