Hello Javier
I don't know what would happen, I installed rails 2.3.9 to try the same code
I wrote in rails 3 and worked like a charm.
There is something weird in your code since the table name for Person model
should be People... I don't know if there is a problem with your
ActiveRecord or maybe a code you are missing to write =S.
Here is my code, now in rails 2.3.9 :
class Person < ActiveRecord::Base
def something
self.name
end
end
class Worker < Person
set_table_name "workers"
end
class Task < ActiveRecord::Base
has_one :worker
end
Worker(id: integer, name: string, task_id: integer, created_at: datetime,
updated_at: datetime)
Person(id: integer, name: string, created_at: datetime, updated_at:
datetime)
Task(id: integer, description: string, created_at: datetime, updated_at:
datetime)
Worker.table_name # => "workers"
w = Worker.create(:name => "WorkerName") # => #<Worker id: 1, name:
"WorkerName", task_id: nil, created_at: "2010-09-14 15:22:55", updated_at:
"2010-09-14 15:22:55">
p = Person.create(:name => "PersonName") # => #<Person id: 1, name:
"PersonName", created_at: "2010-09-14 15:23:01", updated_at: "2010-09-14
15:23:01">
t = Task.create # => #<Task id: 1, description: nil, created_at: "2010-09-14
15:23:16", updated_at: "2010-09-14 15:23:16">
t.worker = w # => #<Worker id: 1, name: "WorkerName", task_id: 1,
created_at: "2010-09-14 15:22:55", updated_at: "2010-09-14 15:23:26">
t.worker.something # => "WorkerName"
Let me know if this worked,please.
Daniel Gaytán
2010/9/14 Javier Ruiz <[email protected]>
> Hi Daniel
>
> Thanks for helping :-)
>
> This is exactly how I was thinking it should work. But I just tried
> using set_table_name and I still got the same result, for example, using
> your code (which is exactly what I'd love to do), I get this:
>
> w = Worker.create(:name => "WorkerName") # ok
> p = Person.create(:name => "PersonName") # ok
> ac = AnotherClass.create # ok
>
> ac.worker = w # here i got the error! like:
> # ActiveRecord::StatementInvalid: Mysql::Error: Unknown column
> 'person.another_class_id' in 'where clause': SELECT * FROM `person`
> WHERE (`person`.another_class_id = 1) LIMIT 1
>
> It still goes to the parent class to search for the relational id.
> "set_table_name" works for you in this particular case? could it be the
> activerecord version I use? My gems are:
>
> actionmailer (2.3.9)
> actionpack (2.3.9)
> activerecord (2.3.9)
> activeresource (2.3.9)
> activesupport (2.3.9)
> builder (2.1.2)
> capistrano (2.5.19)
> highline (1.6.1)
> i18n (0.4.1, 0.3.7)
> json (1.4.6)
> json_pure (1.4.6)
> multi_json (0.0.4)
> mysql (2.8.1)
> net-scp (1.0.3)
> net-sftp (2.0.5)
> net-ssh (2.0.23)
> net-ssh-gateway (1.0.1)
> pg (0.9.0)
> racc (1.4.6)
> rack (1.1.0)
> rails (2.3.9)
> rake (0.8.7)
> rspec (1.3.0)
> text-format (1.0.0)
> text-hyphen (1.0.0)
> tmail (1.2.7.1)
> tzinfo (0.3.23)
> will_paginate (2.3.14)
>
>
> And the trace when I get the error:
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/connection_adapters/abstract_adapter.rb:227:in
> `log'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/connection_adapters/mysql_adapter.rb:324:in
> `execute'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/connection_adapters/mysql_adapter.rb:639:in
> `select'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in
> `select_all_without_query_cache'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in
> `select_all'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/base.rb:665:in
> `find_by_sql'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/base.rb:1582:in
> `find_every'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/base.rb:1539:in
> `find_initial'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/base.rb:617:in
> `find'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/associations/has_one_association.rb:81:in
> `find_target'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/associations/association_proxy.rb:236:in
> `load_target'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/associations/association_proxy.rb:113:in
> `reload'
>
> from
> /usr/lib64/ruby/gems/1.8/gems/activerecord-2.3.9/lib/active_record/associations.rb:1256:in
> `worker'
> from (irb):2
>
> Regards,
>
> Javi Ruiz
>
>
>
> On Mon, 2010-09-13 at 14:30 -0500, Daniel Gaytán wrote:
> > Hello Javier,
> >
> > I was just checking the set_table_name class method of ActiveRecord
> > and I think that could work for you:
> >
> > class AnotherClass < ActiveRecord::Base
> > has_one :worker
> > end
> >
> > class Person < ActiveRecord::Base
> > def something
> > self.name
> > end
> > end
> >
> > class Worker < Person
> > set_table_name "workers"
> > end
> >
> > w = Worker.create(:name => "WorkerName")
> > p = Person.create(:name => "PersonName")
> > ac = AnotherClass.create
> >
> > ac.worker = w
> > ac.save
> >
> > ac.worker.something # => "WorkerName"
> >
> >
> > I hope that would be helpful
> >
> > Daniel Gaytán
> >
> > 2010/9/13 Marnen Laibow-Koser <[email protected]>
> > [Please quote when replying. Otherwise the discussion is
> > impossible to
> > follow.]
> >
> > Javier Ruiz wrote:
> > > Yep that's finally how I will have to do it... but this is
> > not what I
> > > wanted. A has_one relation means that I have to access
> > things like:
> > >
> > > Anotherclass.property
> > > Anotherclass.childclass.specific_property
> > > Anotherclass.childclass.parentclass.common_property
> >
> >
> > That makes sense from a relational point of view. If you
> > don't like it,
> > then use STI.
> >
> > >
> > > And more important... I need to manually manage related
> > objects (or
> > > create hooks os similar). I mean I need for example to do
> > something
> > > like:
> > >
> > > a = Anotherclass.new
> > > b = Childclass.new
> > > a.childclass = b
> > >
> > > ... and so on...
> >
> >
> > You'd need to do that regardless of whether your original idea
> > worked.
> >
> > >
> > > I was thinking "rails' magic" was really magic ;-)
> >
> >
> > I suppose it is, but it doesn't extend to spreading one class
> > across
> > multiple tables.
> >
> > If you were ambitious, you probably *could* extend
> > ActiveRecord to do
> > that, but I'm not sure it's a good idea -- it's trying to
> > impose too
> > much of an OO approach on a relational DB. Alternatively, you
> > could try
> > an OODB like GemStone/MagLev.
> >
> >
> > Best,
> > --
> > Marnen Laibow-Koser
> > http://www.marnen.org
> > [email protected]
> >
> > --
> > Posted via http://www.ruby-forum.com/.
> >
> > --
> > 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
> > [email protected].
> > To unsubscribe from this group, send email to rubyonrails-talk
> > [email protected].
> > For more options, visit this group at
> > http://groups.google.com/group/rubyonrails-talk?hl=en.
> >
> >
> >
> >
> >
> > --
> > 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
> > [email protected].
> > To unsubscribe from this group, send email to rubyonrails-talk
> > [email protected].
> > For more options, visit this group at
> > http://groups.google.com/group/rubyonrails-talk?hl=en.
>
> --
> 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 [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rubyonrails-talk%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>
--
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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.