简体   繁体   中英

Rails 3 build_association

I am attempting to create something along the lines of How to Implement Basic Defensio Spam Protection in Rails but I'm running into a problem. In the article they have something like:

def self.init(attributes)
    self.new(clean!(attributes))
end

private

def self.clean!(attrs)
    returning({}) do |cleansed_attributes|
        attrs.each do |key, value|
            cleansed_attributes.merge!(key.to_s.underscore => value)
        end
    end
end

They are then calling DefensioResponse.init . In my case however I had it setup aa little bit differently. I'm calling comment.build_defensio() (my model is called Defensio instead of DefensioResponse) and it appears that self.init is never being called. How should I change the above code block in order to have it work w/ comment.build_defensio() ?

For More Information

If you need to look at any of the source code I have it hosted over on Github .

Basically what I'm doing is submitting a comment (Skatepark::Comment), after it successfully saves DefensioJob gets enqueued w/ Delayed::Job.enqueue(DefensioJob.new(self.id)) . Inside of the defensio job is where I'm attempting to create and save a new Skatepark::Comment::Defensio record.

You could override initializer of model, like this

def initialize(attributes = {})
  super(clean!(attributes))
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM