Access params in before_validation?


#1

Hey people,

does anyone know if it’s possible to access params inside the
before_validation function?


#2

On 24 Mar 2009, at 10:17, Heinz S. wrote:

Hey people,

does anyone know if it’s possible to access params inside the
before_validation function?

before_validation callbacks are instance methods of models, params is
an instance method of controllers therefore you cannot call params
from a before_validation callback.

Fred


#3

On Tue, Mar 24, 2009 at 5:17 AM, Heinz S.
removed_email_address@domain.invalid wrote:

Hey people,

does anyone know if it’s possible to access params inside the
before_validation function?

Aren’t you doing something like Foo.new( params[:foo] ) already?

params is an array and responds to merge, so you can add stuff to
params before passing it to the model.


Greg D.
http://destiney.com/


#4

Alright, thank you for the info!


#5

Greg D. wrote:

On Tue, Mar 24, 2009 at 5:17 AM, Heinz S.
removed_email_address@domain.invalid wrote:

Hey people,

does anyone know if it’s possible to access params inside the
before_validation function?

Aren’t you doing something like Foo.new( params[:foo] ) already?

params is an array and responds to merge, so you can add stuff to
params before passing it to the model.


Greg D.
http://destiney.com/

I’ve got a similiar problem but need to remove two attributes I only
need for validation.
I have my model plus two attributes (current, required) and I want to
validate these two attributes before saving. I cannot create a new
instance because it says “unknown attribute current”

I could do @errorcheck_current = params[:activity].delete(‘current’) but
then I cannot use it in the model for validation. Does anyone know how
to solve that kind of problem?


#6

Heinz S. wrote:
[…]

I’ve got a similiar problem but need to remove two attributes I only
need for validation.
I have my model plus two attributes (current, required) and I want to
validate these two attributes before saving. I cannot create a new
instance because it says “unknown attribute current”

I could do @errorcheck_current = params[:activity].delete(‘current’) but
then I cannot use it in the model for validation. Does anyone know how
to solve that kind of problem?

Delete the virtual attributes after validation, perchance? The
after_validation_* callbacks might help…

Best,

Marnen Laibow-Koser
http://www.marnen.org
removed_email_address@domain.invalid


#7

Greg D. wrote:

attr_accessor :current, :required


Greg D.
http://destiney.com/

Yep, that’s what I needed. Thanks to both of you!


#8

On Wed, Oct 7, 2009 at 6:39 AM, Heinz S.
removed_email_address@domain.invalid wrote:

I’ve got a similiar problem but need to remove two attributes I only
need for validation.
I have my model plus two attributes (current, required) and I want to
validate these two attributes before saving. I cannot create a new
instance because it says “unknown attribute current”

Sounds like you need to add attr_accessor entries.

attr_accessor :current, :required


Greg D.
http://destiney.com/