Javier Quevedo
2007-Dec-27 12:15 UTC
ActiveRecord + has_and_belongs_to_many: My own little problem ;)
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!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Javier Quevedo
2007-Dec-27 12:48 UTC
Re: ActiveRecord + has_and_belongs_to_many: My own little problem ;)
Come''on people it should be an easy question! You can do it ;) On Dec 27, 1:15 pm, Javier Quevedo <jquev...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Javier Quevedo
2007-Dec-27 15:09 UTC
Re: ActiveRecord + has_and_belongs_to_many: My own little problem ;)
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! On Dec 27, 1:48 pm, Javier Quevedo <jquev...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Come''on people it should be an easy question! You can do it ;) > > On Dec 27, 1:15 pm, Javier Quevedo <jquev...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---