AnApprentice
AnApprentice

Reputation: 110960

Given a record, how to get the record's model?

I have the following in several of my models:

The Poll.rb for instance:

def sanitize_html
    for column in Poll.content_columns  
    end
end

What I want to do is include this in my model_helpers which is included in models with:

include ModelHelpers

Problem is I have the model name hard coded in the method above:

for column in Poll.content_columns

How can I dry this out so instead of Poll, rails automatically finds the self.ModelName?

Thanks

Upvotes: 2

Views: 48

Answers (1)

Dty
Dty

Reputation: 12273

You can do

self.class.content_columns

and it will call the class method, which is exactly what you want to do here. (writing a long winded explanation so this doesn't get turned into a comment. hehe)

Upvotes: 3

Related Questions