Javier Quevedo
2007-Dec-25 11:12 UTC
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? ;)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Xavier Noria
2007-Dec-25 11:28 UTC
Re: Problem with Activerecord and relation has_many belongs_to
On Dec 25, 2007, at 12:12 PM, Javier Quevedo wrote:> 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,Does that column name have a typo?> 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>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 --~--~---------~--~----~------------~-------~--~----~ 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-25 11:35 UTC
Re: Problem with Activerecord and relation has_many belongs_to
Thank you! It sure works fine! I don''t know why but I tought that it was supposed to be that way. On Dec 25, 12:28 pm, Xavier Noria <f...-xlncskNFVEJBDgjK7y7TUQ@public.gmane.org> wrote:> On Dec 25, 2007, at 12:12 PM, Javier Quevedo wrote: > > > 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, > > Does that column name have a typo? > > > 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> > > 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/ClassMet... > > 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--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---