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.
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-03-24 11:17
Hey people,

does anyone know if it's possible to access params inside the
before_validation function?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-03-24 14:19
(Received via mailing list)
On 24 Mar 2009, at 10:17, Heinz Strunk 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
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-03-24 14:31
(Received via mailing list)
On Tue, Mar 24, 2009 at 5:17 AM, Heinz Strunk
<rails-mailing-list@andreas-s.net> 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 Donald
http://destiney.com/
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-03-24 14:51
Alright, thank you for the info!
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-10-07 13:39
Greg Donald wrote:
> On Tue, Mar 24, 2009 at 5:17 AM, Heinz Strunk
> <rails-mailing-list@andreas-s.net> 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 Donald
> 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?
Dd2d775dea75b381edb1bbf0600a0907?d=identicon&s=25 Marnen Laibow-Koser (marnen)
on 2009-10-07 13:47
Heinz Strunk 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
marnen@marnen.org
Aad37b5f7116c8d1f547d23b37566032?d=identicon&s=25 Greg Donald (destiney)
on 2009-10-07 16:00
(Received via mailing list)
On Wed, Oct 7, 2009 at 6:39 AM, Heinz Strunk
<rails-mailing-list@andreas-s.net> 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 Donald
http://destiney.com/
Fdb488647484e40f785dde7134afc6d1?d=identicon&s=25 Heinz Strunk (cojones)
on 2009-10-07 17:09
Greg Donald wrote:
> ...
>
> attr_accessor :current, :required
>
>
> --
> Greg Donald
> http://destiney.com/

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