Forum: Ruby on Rails Rails 2.2, abstract_class, and associations

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
17c1d72be21a2a43be717c6dc1660c7b?d=identicon&s=25 Jim (Guest)
on 2009-02-11 23:10
(Received via mailing list)
In my project, I've created an abstract model that my real models
inherit from.  In my abstract model, I've overridden method_missing so
that I can use attribute names without the field type prefix from the
legacy database, without having to set up alias_attribute for every
attribute in every table.


class LegacyModel < ActiveRecord::Base
  self.abstract_class = true

  def method_missing(symbol, *args)
    ...do some stuff...
  end
end


This all worked fine in Rails 2.0.2.  It still works in Rails 2.2.2,
but now not across a belongs_to association.  It does work across a
has_many association.

NoMethodError: NoMethodError
  from /Library/Ruby/Gems/1.8/gems/activerecord-2.2.2/lib/active_record/
associations/association_proxy.rb:209:in `method_missing'


Anyone have any ideas on either a better way to handle the attribute
names, or where to start looking to figure out how to make this work?

Thanks,

Jim Crate
Que Viva, LLC
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-12 12:11
(Received via mailing list)
On 11 Feb 2009, at 22:09, Jim wrote:
>
> Anyone have any ideas on either a better way to handle the attribute
> names, or where to start looking to figure out how to make this work?
>
Have you also overridden respond_to?

Fred
17c1d72be21a2a43be717c6dc1660c7b?d=identicon&s=25 Jim (Guest)
on 2009-02-12 16:38
(Received via mailing list)
On Feb 12, 6:11 am, Frederick Cheung <frederick.che...@gmail.com>
wrote:

> Have you also overridden respond_to?

No, I haven't.  But I'm curious what I would do with respond_to? that
would affect method_missing?

Jim
17c1d72be21a2a43be717c6dc1660c7b?d=identicon&s=25 Jim (Guest)
on 2009-02-12 20:14
(Received via mailing list)
On Feb 12, 6:11 am, Frederick Cheung <frederick.che...@gmail.com>
wrote:
>
> Have you also overridden respond_to?

I played around with overriding respond_to?, and figured out how to
make this all work.  Apparently, in Rails 2.2, if respond_to? returns
false, method_missing gets called on the association proxy instead of
the base class.  Although, I have to wonder if there's a better way to
accomplish this than through method_missing.  Maybe there's some way
to loop through all attributes and call alias_attribute dynamically
when classes are loaded?

Anyway, thanks for the help.

Jim
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-02-13 20:43
(Received via mailing list)
On Feb 12, 2:13 pm, Jim <jim...@gmail.com> wrote:
> Although, I have to wonder if there's a better way to
> accomplish this than through method_missing.  Maybe there's some way
> to loop through all attributes and call alias_attribute dynamically
> when classes are loaded?
>

There is - try this:

class LegacyModel < ActiveRecord::Base
  column_names.each do |c|
    # figure out the new name
    alias_attribute new_name, c
  end
end

--Matt Jones
17c1d72be21a2a43be717c6dc1660c7b?d=identicon&s=25 Jim (Guest)
on 2009-02-17 20:45
(Received via mailing list)
On Feb 13, 2:43 pm, Matt Jones <al2o...@gmail.com> wrote:
>   column_names.each do |c|
>     # figure out the new name
>     alias_attribute new_name, c
>   end
> end
>
> --Matt Jones

This would work in a regular model, but not a model with
abstract_class set to true.  LegacyModel is an abstract class that
other models inherit from, so I do not have to repeat certain things
which apply to all legacy tables in the database, like setting the
primary key, certain alias_attribute calls, and a few other things.

Jim
This topic is locked and can not be replied to.