This works : class Recipe < ActiveRecord::Base belongs_to :category end But (when I add validation) this does not work : class Recipe < ActiveRecord::Base belongs_to :category validates_length_of :category, :within => 6..20 validates_uniqueness_of :category, :message => "already exists" end thank you -- View this message in context: http://www.nabble.com/why-belongs_to-does-not-like-validation--t1360119.html#a3643911 Sent from the RubyOnRails Users forum at Nabble.com.
On 3/28/06, mlotfi <majidnakit@yahoo.com> wrote:> > > This works : > > class Recipe < ActiveRecord::Base > belongs_to :category > end > > But (when I add validation) this does not work : > > class Recipe < ActiveRecord::Base > belongs_to :category > validates_length_of :category, :within => 6..20 > validates_uniqueness_of :category, :message => "already exists" > end > > thank you:category is not a database attribute. Validations occur only on database attributes, so use :category_id. -- Rick Olson http://techno-weenie.net
mlotfi wrote:> This works : > > class Recipe < ActiveRecord::Base > belongs_to :category > end > > But (when I add validation) this does not work : > > class Recipe < ActiveRecord::Base > belongs_to :category > validates_length_of :category, :within => 6..20 > validates_uniqueness_of :category, :message => "already exists" > endYou need the category validations in the Category model, not in the Recipe model. If you want to validate that there is a valid category for a Recipe you are creating, you want something like validates_associated :category, :message => "no such category" -- Ray
title is an attribute of the table recipes. here is what I have : class Recipe < ActiveRecord::Base belongs_to :category validates_length_of :title, :within => 6..20 validates_uniqueness_of :title, :message => "already exists" end But when I try to add a new recipe I get this error : NoMethodError in Recipe#create Showing app/views/recipe/new.rhtml where line #17 raised: 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.each Extracted source (around line #17): 14: <p> 15: Category:<br/> 16: <select name="recipe[category_id]"> 17: <% @categories.each do |category| %> 18: <option value="<%= category.id %>"> 19: <%= category.name %> 20: </option> RAILS_ROOT: ./script/../config/.. Application Trace | Framework Trace | Full Trace #{RAILS_ROOT}/app/views/recipe/new.rhtml:17 #{RAILS_ROOT}/app/controllers/recipe_controller.rb:11:in `create'' -- View this message in context: http://www.nabble.com/why-belongs_to-does-not-like-validation--t1360119.html#a3649548 Sent from the RubyOnRails Users forum at Nabble.com.
That means that @categories is nil. Did you set it in the controller? On 3/29/06, mlotfi <majidnakit@yahoo.com> wrote:> > title is an attribute of the table recipes. > here is what I have : > > class Recipe < ActiveRecord::Base > belongs_to :category > validates_length_of :title, :within => 6..20 > validates_uniqueness_of :title, :message => "already exists" > end > > But when I try to add a new recipe I get this error : > > NoMethodError in Recipe#create > Showing app/views/recipe/new.rhtml where line #17 raised: > > 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.each > > Extracted source (around line #17): > > 14: <p> > 15: Category:<br/> > 16: <select name="recipe[category_id]"> > 17: <% @categories.each do |category| %> > 18: <option value="<%= category.id %>"> > 19: <%= category.name %> > 20: </option> > > > > RAILS_ROOT: ./script/../config/.. > > Application Trace | Framework Trace | Full Trace > #{RAILS_ROOT}/app/views/recipe/new.rhtml:17 > #{RAILS_ROOT}/app/controllers/recipe_controller.rb:11:in `create'' > > > -- > View this message in context: http://www.nabble.com/why-belongs_to-does-not-like-validation--t1360119.html#a3649548 > Sent from the RubyOnRails Users forum at Nabble.com. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Also, you could use the form helper collection_select to create that select menu: <%= collection_select ''recipe'', ''category_id'', @categories, ''id'', ''name'' %> On 3/29/06, Carl-Johan Kihlbom <kihlbom@gmail.com> wrote:> That means that @categories is nil. Did you set it in the controller? > > On 3/29/06, mlotfi <majidnakit@yahoo.com> wrote: > > > > title is an attribute of the table recipes. > > here is what I have : > > > > class Recipe < ActiveRecord::Base > > belongs_to :category > > validates_length_of :title, :within => 6..20 > > validates_uniqueness_of :title, :message => "already exists" > > end > > > > But when I try to add a new recipe I get this error : > > > > NoMethodError in Recipe#create > > Showing app/views/recipe/new.rhtml where line #17 raised: > > > > 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.each > > > > Extracted source (around line #17): > > > > 14: <p> > > 15: Category:<br/> > > 16: <select name="recipe[category_id]"> > > 17: <% @categories.each do |category| %> > > 18: <option value="<%= category.id %>"> > > 19: <%= category.name %> > > 20: </option> > > > > > > > > RAILS_ROOT: ./script/../config/.. > > > > Application Trace | Framework Trace | Full Trace > > #{RAILS_ROOT}/app/views/recipe/new.rhtml:17 > > #{RAILS_ROOT}/app/controllers/recipe_controller.rb:11:in `create'' > > > > > > -- > > View this message in context: http://www.nabble.com/why-belongs_to-does-not-like-validation--t1360119.html#a3649548 > > Sent from the RubyOnRails Users forum at Nabble.com. > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > >
yes I did, in recipe_controller.rb : class RecipeController < ApplicationController layout "standard-layout" scaffold:recipe def create @recipe = Recipe.new(@params[''recipe'']) @recipe.date = Date.today if @recipe.save redirect_to :action => ''list'' else render_action ''new'' end end def new @recipe = Recipe.new @categories = Category.find_all end def list @category = @params[''category''] @recipes = Recipe.find_all end def edit @recipe = Recipe.find(@params["id"]) @categories = Category.find_all end def delete Recipe.find(@params[''id'']).destroy redirect_to :action => ''list'' end end -- View this message in context: http://www.nabble.com/why-belongs_to-does-not-like-validation--t1360119.html#a3654139 Sent from the RubyOnRails Users forum at Nabble.com.
mlotfi wrote:> yes I did, in recipe_controller.rb : > def edit > @recipe = Recipe.find(@params["id"]) > @categories = Category.find_all > endAnd you have some independent means of knowing that Category.find_all returned some results?
I am sorry I did not understand your question. I am just doing what in the cookbook tutorial, as I said before without validation everything worked fine. -- View this message in context: http://www.nabble.com/why-belongs_to-does-not-like-validation--t1360119.html#a3654728 Sent from the RubyOnRails Users forum at Nabble.com.
mlotfi wrote:> I am sorry I did not understand your question. > I am just doing what in the cookbook tutorial, as I said before without > validation everything worked fine.Are there any categories in the Category table?
the table is called categories it has an id and name columns. -- View this message in context: http://www.nabble.com/why-belongs_to-does-not-like-validation--t1360119.html#a3655016 Sent from the RubyOnRails Users forum at Nabble.com.
mlotfi wrote:> the table is called categories it has an id and name columns.Are there any rows in the table? If there are no rows then the Catgories.find_all in your statement: @categories = Categories.find_all will return nil. -- Ray