mlotfi
March 29, 2006, 7:37am
1
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 .
mlotfi
March 29, 2006, 7:46am
2
On 3/28/06, mlotfi [email protected] wrote:
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 O.
http://techno-weenie.net
mlotfi
March 29, 2006, 8:04am
3
mlotfi wrote:
validates_length_of :category, :within => 6…20
validates_uniqueness_of :category, :message => “already exists”
end
You 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
mlotfi
March 29, 2006, 3:07pm
4
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:
15: Category:
16:
17: <% @categories.each do |category| %>
18:
19: <%= category.name %>
20:
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 .
mlotfi
March 29, 2006, 6:23pm
5
That means that @categories is nil. Did you set it in the controller?
mlotfi
March 29, 2006, 6:29pm
6
Also, you could use the form helper collection_select to create that
select menu:
<%= collection_select ‘recipe’, ‘category_id’, @categories , ‘id’, ‘name’
%>
mlotfi
March 29, 2006, 7:26pm
7
mlotfi wrote:
yes I did, in recipe_controller.rb :
def edit
@recipe = Recipe.find(@params [“id”])
@categories = Category.find_all
end
And you have some independent means of knowing that Category.find_all
returned some results?
mlotfi
March 29, 2006, 7:02pm
8
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
March 29, 2006, 7:36pm
9
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
March 29, 2006, 7:45pm
10
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?
mlotfi
March 29, 2006, 8:00pm
11
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
March 29, 2006, 8:52pm
12
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