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!