Topic: conditional validations using :if => @session['user'].nil?

Is it possible to do this:

validates_presence_of :name, :if => @session['user'].nil?

because it bombs horribly...
with ( NoMethodError in CommentsController#new
You have a nil object when you didn't expect it!

What I'm unsure of is the relationship, if any,
between the session object and ActiveRecord class...

I know it's the controller is what you go through to reach
this model, but the level of abstraction in validations
obscures how to make this connection for me.

Re: conditional validations using :if => @session['user'].nil?

it might work with Proc, which is basically how you create an arbitrary boolean expression for validations. Look in the docs … ml#M000813

validates_presence_of :name :if => { |user| user.signup_step > 2 })

however the parameter to the proc may need to be the model object. I don't know.

Re: conditional validations using :if => @session['user'].nil?

Yeah, it's more of an issue of whether or not @session is available to the ActiveRecord class.  I hate to say it but I think it's time to start digging into the active_record source code.  I really think it's the only way I'm going to get the big picture.

Re: conditional validations using :if => @session['user'].nil?

but what are you trying to do? Rails is so well designed and so well thought out that usually if I find myself fighting it, it's because I'm not doing something as well as it could be done.

@session (and the preferred session, @session is deprecated) is not available to the models.

Re: conditional validations using :if => @session['user'].nil?

Yeah, it is once you understand the rails methodology.

Basically, if a user comes to a page and submits comments
we don't need any additional information, because we know who they are.

If they are a visitor on the other hand, we need a name
and email... so the validator is conditional based on whether or
not they are a user.

If there's an easy rails way to do this (and other things
that involve a user/visitor dichotomy I'd love to know.
I'm also setting a before_filter in the application controller
to set @view (= 'user' or 'visitor') and doing some conditional
stuff in the interface based on that.