Problem with Activerecord and relation has_many belongs_to

Hi. I’m experiencing a little problem with ActiveRecord and relations.
The idea is that I have a lfsearch model:

class Lfsearch < ActiveRecord::Base
has_many :lfresults
end

and a lfresult model:

class Lfresult < ActiveRecord::Base
belongs_to :lfsearch
end

One search has many results.
The tables are:
CREATE TABLE wozzhotdb.lfresults (
ID int(11) NOT NULL auto_increment,
lfserach_id int(11) default NULL,
name varchar(1024) default NULL,
url varchar(1024) default NULL,
imageurl varchar(1024) default NULL,
PRIMARY KEY (ID)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8

and

CREATE TABLE wozzhotdb.lfsearches (
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=7 DEFAULT CHARSET=latin1

I try to run this code:

@lastfmdb = Lfsearch.new (:query => “Hello”)
@lastfmresult = Lfresult.new (:name => “senc”, :url
=>“www.yo.com”, :imageurl => “Hola”)
@lastfmresult.save
@lastfmdb.save
@lastfmdb.add_lfresult (@lastfmresult)

But I get this error:

NoMethodError in GestorController#parse

undefined method `add_lfresult’ for #<Lfsearch id: 6, query: “Indie”,
timestamp: nil>

Thanks for your help, Any ideas? :wink:

On Dec 25, 2007, at 12:12 PM, Javier Q. wrote:

One search has many results.
The tables are:
CREATE TABLE wozzhotdb.lfresults (
ID int(11) NOT NULL auto_increment,
lfserach_id int(11) default NULL,

Does that column name have a typo?

NoMethodError in GestorController#parse

undefined method `add_lfresult’ for #<Lfsearch id: 6, query: “Indie”,
timestamp: nil>

Active Record provides an API to manage collections from their parent,
please read the documentation in

http://api.rubyonrails.com/classes/ActiveRecord/Base.html
http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html

In that case you typically write something like

@lastfmdb.results.build(:name => “senc”, :url
=>“www.yo.com”, :imageurl => “Hola”)
@lastfmdb.save # saves associated results if successful

– fxn

Thank you! It sure works fine! I don’t know why but I tought that it
was supposed to be that way.