In a model, it seems that methods for column access are only created
when one column method on an object is accessed. So, how can one
override the default column methods?
For instance:
create_table “dogs”, :force => true do |t|
t.column “color”, :string
end
create_table “tails”, :force => true do |t|
t.column “dog_id”, :integer, :null => false
t.column “color”, :string
end
If a dog’s tail indicates its own color, I want to use it. Otherwise,
the tail’s color is the same as the dog’s color:
class Dog < AR::Base
has_one :tail
end
class Tail < AR::Base
belongs_to :dog
alias_method :tail_color, :color
def color
return tail_color if tail_color != nil
return dog.tail_color
end
end
The trouble is that the ‘color’ method doesn’t exist yet:
$ ./script/console
Loading development environment.
x = Tail.new
NameError: undefined methodcolor' for class
Tail’
from script/…/config/…/config/…/app/models/tail.rb:4:in
`alias_method’
Is there a way to force pre-defining of column methods so I can redefine
them?