# Rake db:migrate tells me a custom validation is not avail.!

**URL:** <https://www.ruby-forum.com/t/rake-db-migrate-tells-me-a-custom-validation-is-not-avail/117548>\
**Category:** Rails\
**Created:** [October 17, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/rake-db-migrate-tells-me-a-custom-validation-is-not-avail/117548 "2007-10-17T23:00:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 17, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/rake-db-migrate-tells-me-a-custom-validation-is-not-avail/117548/1 "2007-10-17T23:00:12Z")

</div>

Hi all

I have the following custom validation helper in application.rb:

module ActiveRecord::Validations::ClassMethods  
def validates\_email\_format\_of(\*attr\_names)  
configuration = {}  
configuration.update(attr\_names.pop) if attr\_names.last.is\_a?(Hash)

```
validates_each(attr_names.first.to_s) do |record, attr_name, value|
  unless configuration[:if] and not

```

evaluate\_condition(configuration[:if], record) # Evaluate :if option  
next if configuration[:allow\_nil] and  
record.send("#{attr\_name}\_before\_type\_cast").nil? # Skip this one if  
emptyness is allowed and the email attribute is empty

```
    if record.send(attr_name.to_sym) !~

```

/^([a-z0-9.-Ã¤Ã¶Ã¼Ã¨Ã Ã©Ãª\_])+@([a-z0-9.-Ã¤Ã¶Ã¼Ã¨Ã Ã©Ãª\_])+.([a-z]{2,})$/i #  
Check if the email is valid  
record.errors.add(attr\_name,  
ActiveRecord::Errors::default\_error\_messages[:invalid\_email\_format])  
end  
end  
end  
end  
end

In my model Member I call validates\_email\_format\_of :email.

class Member \< ActiveRecord::Base  
…  
validates\_email\_format\_of :email  
…  
end

When starting script/console there’s no problem - When no email or a  
wrong email is supplied, I get the appropriate error message added to  
the model instance.

However, when adding the following migration…

class AddCreatorAndOwner \< ActiveRecord::Migration  
def self.up  
# Check if there is a member with the ID 1 which is supposed to be  
the webmaster  
raise “There is no member with the ID 1 (Webmaster)!” unless  
Member.find(1)

```
# Add the new column :creator_id which holds as default the ID 1

```

(webmaster)  
add\_column :music\_artists, :creator\_id, :integer, :null =\> false,  
:default =\> 1, :references =\> :members, :on\_delete =\> :restrict

```
# Alter the field so for future management of music artists one has

```

to specify a creator\_id (1 is not default anymore!)  
execute(“ALTER TABLE music\_artists MODIFY creator\_id INTEGER(11) NOT  
NULL”)  
end

def self.down  
drop\_column :music\_artists, :creator\_id  
end  
end

…I always get the following error:

# 192:~/Sites/projects/export josh$ rake db:migrate /usr/local/bin/rake:17:Warning: require\_gem is obsolete. Use gem instead. (in /Users/josh/Sites/projects/export) == AddCreatorAndOwner: migrating

rake aborted!  
undefined method `validates\_email\_format\_of’ for Member:Class

(See full trace by running task with --trace)

What’s wrong here? Why won’t the Member class find the  
validates\_email\_format\_of method in the migration, but when using it in  
the console it does?

Thanks a lot for help.  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 18, 2007, 10:49am UTC](https://www.ruby-forum.com/t/rake-db-migrate-tells-me-a-custom-validation-is-not-avail/117548/2 "2007-10-18T10:49:29Z")

</div>

Hrm maybe application.rb is the wrong place for custom validation  
helpers? Where should I put them? Or is it because of something else…?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 18, 2007, 11:08am UTC](https://www.ruby-forum.com/t/rake-db-migrate-tells-me-a-custom-validation-is-not-avail/117548/3 "2007-10-18T11:08:24Z")

</div>

Joshua M. wrote:

> Hrm maybe application.rb is the wrong place for custom validation  
> helpers? Where should I put them? Or is it because of something else…?

Well, I moved it now to  
vendor/plugins/some\_plugin/lib/custom\_validations.rb

and load it in vendor/plugins/some\_plugin/init.rb, and now it works. No  
idea why it didn’t before, however, this way it is cleaner, anyway. 😉
