On 26 January 2012 17:08, Mohamad El-Husseini <husseini....@gmail.com> wrote: > For the sake of example let's say you have an application where users list > their ducks for sale. The services offers two Plans: Bronze, which allows > you to list 3 ducks at a time; and Platinum, which allows you to list an > unlimited number of ducks. > > To handle this logic in the model we add a custom validation to check if > user.current_ducks_listed > user.plan.max_number_of_ducks, and throw an > error accordingly. > > Ideally, we don't want the problem to get this far. It's best to prevent the > user from adding a duck if he has reached his limit.
Firstly, don't have a current_ducks_listed attribute in the database, just use @user.ducks.count. Then have a validation in the Duck model that prevents the duck from being saved if its owner already has his max allowance. 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@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.