Forum: Ruby on Rails why belongs_to does not like validation?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
mlotfi (Guest)
on 2006-03-29 09:37
(Received via mailing list)
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...
Sent from the RubyOnRails Users forum at Nabble.com.
Rick O. (Guest)
on 2006-03-29 09:46
(Received via mailing list)
On 3/28/06, mlotfi <removed_email_address@domain.invalid> 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
Ray B. (Guest)
on 2006-03-29 10:04
(Received via mailing list)
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 (Guest)
on 2006-03-29 17:07
(Received via mailing list)
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...
Sent from the RubyOnRails Users forum at Nabble.com.
Carl-Johan K. (Guest)
on 2006-03-29 20:23
(Received via mailing list)
That means that @categories is nil. Did you set it in the controller?
Carl-Johan K. (Guest)
on 2006-03-29 20:29
(Received via mailing list)
Also, you could use the form helper collection_select to create that
select menu:

<%= collection_select 'recipe', 'category_id', @categories, 'id', 'name'
%>
mlotfi (Guest)
on 2006-03-29 21:02
(Received via mailing list)
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...
Sent from the RubyOnRails Users forum at Nabble.com.
Ray B. (Guest)
on 2006-03-29 21:26
(Received via mailing list)
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 (Guest)
on 2006-03-29 21:36
(Received via mailing list)
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...
Sent from the RubyOnRails Users forum at Nabble.com.
Ray B. (Guest)
on 2006-03-29 21:45
(Received via mailing list)
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 (Guest)
on 2006-03-29 22:00
(Received via mailing list)
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...
Sent from the RubyOnRails Users forum at Nabble.com.
Ray B. (Guest)
on 2006-03-29 22:52
(Received via mailing list)
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
This topic is locked and can not be replied to.