[Rails] [DEFECT] 'validates_inclusion_of' not working as expected (couldn't post to Trac as it gave me an error)

Steve Kellock skellock at gmail.com
Wed Feb 9 16:52:20 GMT 2005


Ya...

It has to do with the presence of .inspect on line 270 of
activerecord/lib/active_record/validations.rb.

A workaround could be:

  validates_inclusion_of :project_id, :in => Project.find_all.collect{
|p| p.id }

But if that scares you (which it may if you have no projects), you'll
have to resort to old-school custom validation.

I'll check out some alternatives to using .inspect as soon as I have
time.  Hopefully you or somebody will beat me to the punch tho... ;-)

Steve

On Wed, 9 Feb 2005 11:09:20 -0500, John Wilger <johnwilger at gmail.com> wrote:
> Hello,
> 
> I tried to post this as a bug on the trac site, but whenever I hit
> submit, I got the error message:
> 
> ---
> Precondition Failed
> 
> The precondition on the request for the URL /newticket evaluated to false.
> ---
> 
> Anyway...
> 
> Let's say I have the following model classes:
> 
> ---
> class Project < ActiveRecord::Base
>   has_many :tasks
> end
> 
> class Task < ActiveRecord::Base
>   belongs_to :project
>   validates_inclusion_of :project, :in Project.find_all
> end
> ---
> 
> You would expect the validation to work properly (Project.find_all
> returns an enumerable), but Ruby complains of a syntax error when
> performing the validation callback:
> 
> ---
> 1) Error:
> test_create(TasksControllerTest):
> SyntaxError: compile error
> /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/validations.rb:378:
> syntax error
> errors.add("project", "is not included in the list") unless
> ([#<Project:0xb7d47a88 @attributes={"name"=>"First Project",
> "id"=>"1", "archived"=>"0", "description"=>"The first project.",
> "default_iteration_length"=>"14"}>, #<Project:0xb7d47a24
> @attributes={"name"=>"Second Project", "id"=>"2", "archived"=>"0",
> "description"=>"The second project",
> "default_iteration_length"=>"14"}>]).include?(project)
>        ^
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/validations.rb:378:in
> `run_validations'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/validations.rb:378:in
> `run_validations'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/validations.rb:374:in
> `each'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/validations.rb:374:in
> `run_validations'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/validations.rb:337:in
> `valid_without_callbacks'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/callbacks.rb:294:in
> `valid?'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/validations.rb:322:in
> `save_without_transactions'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/transactions.rb:121:in
> `save'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/transactions.rb:121:in
> `transaction'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/transactions.rb:87:in
> `transaction'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/transactions.rb:113:in
> `transaction'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/transactions.rb:121:in
> `save'
>     ./test/functional/../../config/..//app/controllers/iterations_controller.rb:21:in
> `create'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/base.rb:607:in
> `send'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/base.rb:607:in
> `perform_action_without_filters'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/filters.rb:294:in
> `perform_action_without_benchmark'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/benchmarking.rb:30:in
> `perform_action_without_rescue'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/benchmarking.rb:30:in
> `measure'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/benchmarking.rb:30:in
> `perform_action_without_rescue'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/rescue.rb:75:in
> `perform_action'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/base.rb:274:in
> `send'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/base.rb:274:in
> `process'
>     /usr/local/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/test_process.rb:246:in
> `process'
>     ./test/functional/tasks_controller_test.rb:42:in `test_create'
> ---
> --
> Regards,
> John Wilger
> 
> -----------
> Alice came to a fork in the road. "Which road do I take?" she asked.
> "Where do you want to go?" responded the Cheshire cat.
> "I don't know," Alice answered.
> "Then," said the cat, "it doesn't matter."
> - Lewis Carrol, Alice in Wonderland
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


More information about the Rails mailing list