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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2007-Aug-14 11:40 UTC
Re: Completely un-understandable behavior in a simple Model
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
K. Rajesh
2007-Aug-18 07:05 UTC
Re: Completely un-understandable behavior in a simple Model
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 On Aug 14, 4:40 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
K. Rajesh
2007-Aug-18 07:12 UTC
Re: Completely un-understandable behavior in a simple Model
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 On Aug 18, 12:05 pm, "K. Rajesh" <kaviraj...-uAjRD0nVeow@public.gmane.org> wrote:> 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 > > On Aug 14, 4:40 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote: > > > 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Isak Hansen
2007-Aug-18 07:54 UTC
Re: Completely un-understandable behavior in a simple Model
On 8/18/07, K. Rajesh <kavirajesh-uAjRD0nVeow@public.gmane.org> 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> see such an accurate prediction! > > Regards, > Rajesh > > On Aug 14, 4:40 pm, Mark Reginald James <m...-bzGI/hKkdgQnC9Muvcwxkw@public.gmane.org> wrote: > > 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. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---