On 17 November 2011 19:26, Angelo Cordova <[email protected]> wrote: > the validation is for quantity of products, the idea is, if the user > try to enter a quantity greater than the available quantity of > products, then the validation error must show up. > > The problem is, products are in differents storages, and storages > belongs to a branch (a company or enterprise has a main office and has > many branches). so, I have to start my validation finding the storages > that belongs to a certain branch, when I have the storages I have to > find the product and get the total quantity of that product. > > The thing is that, to get the branch id, I need to access to > "session[:user].branch_id", but I can't do that in the model.
Can't you get at the branch via the invoice? > > Can you give me, please, an example of the last option > > "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." > > I don't know how to add a non-database attribute in a _partial for > nested attributes, neither how to set it prior to saving Actually you probably can't do this directly when you are using accepts nested attributes as you don't have a point in the controller at which you can set the variable before saving. However, if the branch is not available already via the invoice then provide a class member variable of Invoice (look up member variables and attr_accessor if you don't know how to do this, note, not attr_accessible), and set the branch id there before the save or update_attributes in the controller. However, as I suggested above, I would have thought the invoice would already contain information about the branch. By the way, it is better to interleave your reply with the previous post rather than putting it at the top, then you will not have to repeat sections from my post as you did above, and it will be easier to follow the thread. Colin > On 17 nov, 13:59, Colin Law <[email protected]> wrote: >> On 17 November 2011 16:46, Angelo Cordova <[email protected]> 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 [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- gplus.to/clanlaw -- 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

