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 method `color'' 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?
--
Posted via http://www.ruby-forum.com/.
I''m sure I''ve done something similar in the past. Maybe try alias in place of alias_method... alias :tail_color :color def color ... end Does that make any difference (I am unable test at present)? Chris On 8/16/06, Dan Kirkwood <dangogh@gmail.com> wrote:> 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 method `color'' 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? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
No.. that was actually what I attempted first. I get the same error. Chris Roos wrote:> I''m sure I''ve done something similar in the past. > > Maybe try alias in place of alias_method... > > alias :tail_color :color > def color ... end > > Does that make any difference (I am unable test at present)? > > Chris-- Posted via http://www.ruby-forum.com/.
In case someone else was looking for this: you can get to the raw data using read_attribute(...) or the short form: self[:color] e.g. class Dog <... def color self[:color] || dog[:color] end end -- Posted via http://www.ruby-forum.com/.