Hi all, I am a newbie to Rails. Please enlighten me on how to do this appropriately, the Rails and the Ruby way: Suppose I have a Recipe model. Let's simplify things and pretend that it has only 2 attributes, a :name and the other is a 'category_id'. In the recipes table, category_id is a foreign key to field id of table categories. We also assume that I have generate the appropriate scaffolding code for Recipe and Category. All goes well if I go to http://localhost:3000/categories (I'm using WEBrick) and add a new Category and then go to http://localhost:3000/recipes and add a Recipe and choose a category for the recipe. What if I go to http://localhost:3000/recipes first and add a new recipe without specifying a category (note that I enforce the category_id field in the recipes table to be not null). Rails will definitely complain. Is there somekind of "validates_presence_of" for *_id kinds of field thus the user of my application can be notified to make a category first before adding recipe? Tq very much, John
on 2006-01-03 08:34
on 2006-01-03 08:59
John, Check out 'validates_associated' http://api.rubyonrails.com/classes/ActiveRecord/Validations/ ClassMethods.html#M000689 Chris Nolan.ca http://kweschun.com/ - Do you have a kweschun?
on 2006-01-03 12:13
On 1/3/06, Chris Nolan.ca <firstname.lastname@example.org> wrote: > John, > Check out 'validates_associated' > Chris Nolan.ca Hey, thanks for the response. Thanks for introducing me to validates_associated too. However, after updating my Recipe model with: validates_associated :category , the error is still there. I'm afraid that this doesn't have anything to do with validating association, here's the error: "You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occured while evaluating nil.inject" I think this is because I use <%= collection_select 'recipe', 'category_id', @categories, 'id', 'description' %> in _form.rhtml and by the time I intentionally create a new recipe, there is not even a single category. How to handle this the Rails way? Tq.
on 2006-01-03 13:39
on 2006-01-03 19:28
Chris Hall wrote: > in your Recipe model, you just do: > > validates_presence_of :name, :category_id Actually, I suggest you use validates_presence_of :category Because the category association will use the category_id column to try and get the Category object, you'll end up checking if the category_id is not nil and if it is not nil, it'll also try to load the associated row from the Category table. If that row doesn't exist for the category_id, then you'll also get a validation error. So you get two checks for one line of code. Regards, Blair -- Blair Zajac, Ph.D. <email@example.com> Subversion and Orca training and consulting http://www.orcaware.com/svn/