# Habtm - suppress validation on associated class?

**URL:** <https://www.ruby-forum.com/t/habtm-suppress-validation-on-associated-class/130546>\
**Category:** Rails\
**Created:** [February 29, 2008, 7:09am UTC](https://www.ruby-forum.com/t/habtm-suppress-validation-on-associated-class/130546 "2008-02-29T07:09:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddesgn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@ddesgn](https://www.ruby-forum.com/u/ddesgn)\
**Post date:** [February 29, 2008, 7:09am UTC](https://www.ruby-forum.com/t/habtm-suppress-validation-on-associated-class/130546/1 "2008-02-29T07:09:00Z")

</div>

When I create a new pet, the pet is associated with a User through  
habtm. These are the classes:

class Pet \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
end

class User \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :pets  
validates\_uniqueness\_of :screen\_name, :email  
end

This is the code in the controller for adding the new pet, which updates  
the join table:

@pet = Pet.new(params[:pet])  
@user = User.find\_by\_id(user\_id)  
@pet.users \<\< @user  
@pet.save

This makes a lot of calls to the database:

SHOW FIELDS FROM `users`  
SELECT \* FROM `users` WHERE (`users`.`id` = ‘11’) LIMIT 1  
BEGIN  
COMMIT  
BEGIN  
(these next two lines are validations)  
SELECT \* FROM `users` WHERE (users.screen\_name = ‘chairs’ AND users.id  
\<\> 11) LIMIT 1  
SELECT \* FROM `users` WHERE (users.email = ‘removed\_email\_address@domain.invalid’ AND users.id  
\<\> 11) LIMIT 1  
INSERT INTO `pets` (`name`, `updated_at`, `description`, `created_at`)  
VALUES(‘Kitty Kitty’, ‘2008-02-28 18:08:36’, ‘Lost cat in Washington.’,  
‘2008-02-28 18:08:36’)e  
INSERT INTO users\_pets (`user_id`, `pet_id`) VALUES (11, 2)e  
COMMIT

Is there any way of reducing the number of calls to the database? Is  
there a way to suppress the validation calls? I also don’t understand  
why it makes the first BEGIN/COMMIT calls. Or is there a better way to  
do this that is more efficient? I could do custom SQL commands, but I  
was hoping to do it the rails way.
