Overwriting ActiveRecord::Base.content_columns

2 views
Skip to first unread message

Jan

unread,
Sep 10, 2006, 8:42:42 AM9/10/06
to rubyonra...@googlegroups.com
Hi guys,

I want to define my own Content_colums methode as part of the
ActiveRecord::Base class.

To do so, I have added the following code:

[Environment.rb]
require 'model_extensions'
ActiveRecord::Base.send(:extend, ModelExtensions)

[model_extensions.rb]
module ModelExtensions
#Define Mixins for the ActiveRecord::Base modelclass
# Define list of available items for select
def valid_items(current_item=nil)
#.... works fine
end

# Overwrite displayble columns attribute, eliminate: created_* and
updated_*
def content_columns
@content_columns ||= columns.reject { |c| c.primary || c.name =~
/(_id|_count)$/ || c.name == inheritance_column || c.name ==
locking_column || c.name =~ /(created_|updated_)/}
end
end


However, this won't work for the content_columns! The valid_items method
though works perfectly as it is added to the AR:Base class.
But overwriting content_columns has no effect at all.
I tried to put 'attr :content_columns, true' but no effect neither


Any clues?

--
Posted via http://www.ruby-forum.com/.

Mark Reginald James

unread,
Sep 11, 2006, 5:53:14 AM9/11/06
to rubyonra...@googlegroups.com
Jan wrote:

extend will only add methods, not re-define existing ones.

Instead, just write model_extensions.rb as

class ActiveRecord::Base
def self.valid_items(current_item=nil)
...
end

def self.content_columns
...
end
end

and require this in environment.rb

--
We develop, watch us RoR, in numbers too big to ignore.

Reply all
Reply to author
Forward
0 new messages