Elmor
Elmor

Reputation: 4895

validate presence of hash element & times when hash is not set?

I'm validating on one stage

params[:user][:address]

But there are times when params[:user] are not set. What should I use to check against this? blank?, nil?, or present?? The objective is to make this check on as few lines as possible.

Upvotes: 0

Views: 446

Answers (2)

Elmor
Elmor

Reputation: 4895

with arrays

unless @banners.try(:[], 2)

with objects should try )

Upvotes: 0

Alex D
Alex D

Reputation: 30445

unless params[:user] && params[:user][:address].present?
  # do something
end

Upvotes: 1

Related Questions