Re: [Rails] Access a “session” variable in a nested model

Colin Law
Thu, 17 Nov 2011 16:59:41 -0000

On 17 November 2011 16:46, Angelo Cordova
<acordinz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> Hello people.
>
> I have a big problem here. In my rails 3.0.9 app I have a model
> "invoice" that model "accepts nested attributes" for
"invoice
> details". So, I don''t have a "controller" nor
"views" for "invoice
> details", just model, because I have a "partial" in
"invoices views"
> for the "nested attributes".
>
> The thing is that I need to run a custom validation when every
> "invoice details" is created, and I need a variable stored in
> "session". But Models can not access "session"
variables.
>
> So I want to know, how can I access that variable and then use it in
> my validation.

Is the custom validation to do with data in the invoice?  I believe
you can access the invoice that the details belong to in the
validation.

Also possibly reject_if on accepts_nested_attributes_for might do the job.

If all else fails you could add a non-database attribute to invoice
details and set that prior to saving, then pick it up in the
validation.

Colin

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org