Forum: Ruby on Rails Access params in before_validation?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Heinz S. (Guest)
on 2009-03-24 12:17
Hey people,

does anyone know if it's possible to access params inside the
before_validation function?
Frederick C. (Guest)
on 2009-03-24 15:19
(Received via mailing list)
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
Greg D. (Guest)
on 2009-03-24 15:31
(Received via mailing list)
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/
Heinz S. (Guest)
on 2009-03-24 15:51
Alright, thank you for the info!
Heinz S. (Guest)
on 2009-10-07 15:39
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?
Marnen L. (Guest)
on 2009-10-07 15:47
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
Greg D. (Guest)
on 2009-10-07 18:00
(Received via mailing list)
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/
Heinz S. (Guest)
on 2009-10-07 19:09
Greg D. wrote:
> ...
>
> attr_accessor :current, :required
>
>
> --
> Greg D.
> http://destiney.com/

Yep, that's what I needed. Thanks to both of you!
This topic is locked and can not be replied to.