views:

18

answers:

2

I'm attempting to create a custom validation for one of my models in Rails 2.3.5, but I keep recieving the following error everytime I run my testing suite:

`method_missing_without_paginate': undefined local variable or method `validates_progression'

app/models/project.rb

class Project < ActiveRecord::Base
   ...
   validates_progression

   def validates_progression
      true # stubtastic!
   end
end

I can't seem to make much of this~

A: 

It doesn't work because you are defining a method with instance scope and you are trying to call it within the class scope. You have two alternatives:

Instance Scope

class Project < ActiveRecord::Base

  validate :validates_progression

  def validates_progression
     true # stub
  end

end

Class scope

class Project < ActiveRecord::Base

  def self.validates_progression
     true # stub
  end

  # Be sure to define this method before calling  it
  validates_progression

end

The second alternative doesn't really makes sense unless you want to wrap an other filter.

class Project < ActiveRecord::Base

  def self.validates_progression
     validates_presence_of :progression
     validates_length_of ...
  end

  # Be sure to define this method before calling  it
  validates_progression

end

Otherwise, go with the first solution.

Simone Carletti
Huh for some reason I thought ruby wasn't prone to this type of scoping error. Thanks!
kelly.dunn
A: 

The pagination reference is a red herring. The clue is the 'without'. The will paginate gem has aliased the existing method_missing method and called it method_missing_without_pagination. So, the problem is a standard missing method error.

The method is missing because it is a) not defined when you call it and b) not in the correct cope (you are trying to call an instance method in the scope of the class).

You can add your custom validation by using validate with the symbol for your validation method:

validate :validates_progression

def validates_progression
  true
end
Shadwell