# ActiveRecord + has\_and\_belongs\_to\_many: My own little problem ;)

**URL:** <https://www.ruby-forum.com/t/activerecord-has-and-belongs-to-many-my-own-little-problem/124295>\
**Category:** Rails\
**Created:** [December 27, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/activerecord-has-and-belongs-to-many-my-own-little-problem/124295 "2007-12-27T13:16:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Javier\_Q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Javier\_Q](https://www.ruby-forum.com/u/Javier_Q)\
**Post date:** [December 27, 2007, 1:16pm UTC](https://www.ruby-forum.com/t/activerecord-has-and-belongs-to-many-my-own-little-problem/124295/1 "2007-12-27T13:16:30Z")

</div>

The models:  
class Search \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :dcresults

class Dcresult \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :searches

The code:

@links = Array.new  
@titles = Array.new

#build search results in the arrays  
…

#results built

@titles.each\_index do |index|  
if !Dcresult.exists?(:title =\> @titles[index], :link=\>  
@links[index])  
search.dcresults.build(:title =\> @titles[index], :link =\>  
@links[index])  
else  
@dcresult = Dcresult.find (:first, :conditions =\> [ “link  
= ?”, @links[index]])  
#@dcresult = Dcresult.  
search.dcresults\<\<@dcresult  
end

Abstract: I build the results in two arays. Then i loop the arrays and  
check if a certain result already exists. If the result already exists  
I don’t want to build a new result. I want to associate the already  
existing result to the new search.

Error:  
Mysql::Error: #23000Column ‘dcresult\_id’ cannot be null: INSERT INTO  
dcresults\_searches (`search_id`, `dcresult_id`) VALUES (204, NULL)

SQL TABLES:  
CREATE TABLE `wozzhotdb`.`searches` (  
`id` int(11) NOT NULL auto\_increment,  
`query` varchar(1048) default NULL,  
`timestamp` timestamp NOT NULL default CURRENT\_TIMESTAMP on update  
CURRENT\_TIMESTAMP,  
PRIMARY KEY (`id`)  
) ENGINE=MyISAM AUTO\_INCREMENT=205 DEFAULT CHARSET=latin1

CREATE TABLE `wozzhotdb`.`dcresults` (  
`ID` int(11) NOT NULL auto\_increment,  
`title` varchar(1024) default NULL,  
`link` varchar(1024) default NULL,  
PRIMARY KEY (`ID`)  
) ENGINE=MyISAM AUTO\_INCREMENT=2574 DEFAULT CHARSET=latin1

CREATE TABLE `wozzhotdb`.`dcresults_searches` (  
`search_id` int(11) NOT NULL,  
`dcresult_id` int(11) NOT NULL  
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Thanks for your help in advance!

---

<div class="post-metadata">

**Author:** ![Javier\_Q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Javier\_Q](https://www.ruby-forum.com/u/Javier_Q)\
**Post date:** [December 27, 2007, 1:49pm UTC](https://www.ruby-forum.com/t/activerecord-has-and-belongs-to-many-my-own-little-problem/124295/2 "2007-12-27T13:49:51Z")

</div>

Come’on people it should be an easy question! You can do it 😉

---

<div class="post-metadata">

**Author:** ![Javier\_Q](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@Javier\_Q](https://www.ruby-forum.com/u/Javier_Q)\
**Post date:** [December 27, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/activerecord-has-and-belongs-to-many-my-own-little-problem/124295/3 "2007-12-27T18:22:35Z")

</div>

I already fixed it. The code was OK but the problem was on the  
databases (i Guess). I redid the databases with migrations and now it  
works.  
Yuupi!
