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.
Jim (Guest)
on 2009-02-12 00: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
Frederick C. (Guest)
on 2009-02-12 13: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
Jim (Guest)
on 2009-02-12 17:38
(Received via mailing list)
On Feb 12, 6:11 am, Frederick C. <removed_email_address@domain.invalid>
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
Jim (Guest)
on 2009-02-12 21:14
(Received via mailing list)
On Feb 12, 6:11 am, Frederick C. <removed_email_address@domain.invalid>
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
Matt J. (Guest)
on 2009-02-13 21:43
(Received via mailing list)
On Feb 12, 2:13 pm, Jim <removed_email_address@domain.invalid> 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 J.
Jim (Guest)
on 2009-02-17 21:45
(Received via mailing list)
On Feb 13, 2:43 pm, Matt J. <removed_email_address@domain.invalid> wrote:
>   column_names.each do |c|
>     # figure out the new name
>     alias_attribute new_name, c
>   end
> end
>
> --Matt J.

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.