andy
andy

Reputation: 71

Ruby on Rails: "after_create" and validations

I have a record that needs to be validated before doing some action. Am I required to use a "valid?" method if I'm doing it with after_create?

For example, I have in my User model:

def after_create
  if valid?
  ...
  end
end

I thought it wasn't necessary to put in the valid method, but my application is telling me otherwise. Any idea?

Upvotes: 0

Views: 1858

Answers (1)

bensie
bensie

Reputation: 5403

You do not need the if valid? declaration there because after_create gets called after the record has already been validated (and created).

What do you mean your application is telling you otherwise?

Also, for the callback methods, you should use something like:

after_create :call_my_method

private

def call_my_method
  # Do cool stuff
end

Upvotes: 4

Related Questions