AdamNYC
AdamNYC

Reputation: 20445

Validation before saving object to session

I have a Post object that I would like to save to session[:post].

In my Post.rb, I have:

class Post < ActiveRecord::Base
  validates :link, presence: true, url: true

I would like to use this validation before I save my post object to session (not database). Is there a way to do that?

Thank you.

Upvotes: 0

Views: 553

Answers (2)

pierallard
pierallard

Reputation: 3371

Call the method post.valid? on your object before saving it into your session ?

Upvotes: 1

Mori
Mori

Reputation: 27789

post = Post.new(args)
if post.valid?
  # save to session
end

Upvotes: 3

Related Questions