Eric Sloane
2005-Dec-07 06:33 UTC
Desperate Newbie Question: Going round in circles trying to save a HABTM
Hi, Firstly, I apologise for the repost - but I''m getting desperate for a solution. I have a couple of tables, Jobs and Addresses with a HABTM relationship through Jobs_Addresses and I have a model and controller for both jobs and addresses. What would be the best way to code a create method to save the two data sets? In the JoController I tried; def new @job = Job.new @address = Address.new end def create @job = Job.new(params[:job]) @address = Address.new(params[:address]) if @job.save flash[:notice] = ''Job was successfully created.'' redirect_to :action => ''list'' else render :action => ''new'' end end Which resulted in; NoMethodError in Job#create undefined method `address'' for #<Job:0x3912190> Request Parameters: {"commit"=>"Save Job Data", "job"=>{"job_number"=>"1984", "closed"=>"false", "job_phase_id"=>"1", "job_type_id"=>"1"}, "address"=>{"country"=>"Australia", "suburb"=>"Spanish Harlem", "address_1"=>"Level 42", "address_2"=>"77, Sunset Strip", "state"=>"Confused"}} So all the data is there I just don''t know how to split the save to two tables. Sorry it''s such a dumb question, but I''ve been trying to make this work for a couple of days now. Help much appreciated Kind Regards, Eric.
Peter Donald
2005-Dec-07 07:41 UTC
Re: Desperate Newbie Question: Going round in circles trying to save a HABTM
Hi, You are not giving enough information for anyone to help solve the issue. Try sending along a stack trace, version of rails, relevent snippets of the model code (where stack trace originatest) etc and we may be able to give you better advice ;) -- Cheers, Peter Donald Blog: http://www.RealityForge.org
Eric Sloane
2005-Dec-07 09:16 UTC
Re: Desperate Newbie Question: Going round in circles trying to save a HABTM
Peter Donald wrote:> Hi, > > You are not giving enough information for anyone to help solve the > issue. Try sending along a stack trace, version of rails, relevent > snippets of the model code (where stack trace originatest) etc and we > may be able to give you better advice ;) > > -- > Cheers, > > Peter Donald > > Blog: http://www.RealityForge.orgHmm Peter, OK - thanks for your really helpful response!! Next time - at the very least - you could perhaps post a quote of the original question along with your critisism. That way your input could be evaluated beside the problem! As you say - Cheers
Peter Donald
2005-Dec-07 09:30 UTC
Re: Re: Desperate Newbie Question: Going round in circles trying to save a HABTM
On 12/7/05, Eric Sloane <esloane-9MXrTL7Q3obvnOemgxGiVw@public.gmane.org> wrote:> Hmm Peter, OK - thanks for your really helpful response!! Next time - at > the very least - you could perhaps post a quote of the original question > along with your critisism. That way your input could be evaluated beside > the problem!Easy tiger. I was only trying to help you get an answer to your question. Specifically I asked for you to provide more information so that I could help you. Specifically "a stack trace, version of rails, relevent snippets of the model code" none of which you seem to have provided. Others may be able to help you with your problem with what you have provided but I certainly can not. As a side note you may want to have a read through "How To Ask Questions The Smart Way" [1] if you want to get more useful response. [1] http://www.catb.org/~esr/faqs/smart-questions.html -- Cheers, Peter Donald Blog: http://www.RealityForge.org
Deirdre Saoirse Moen
2005-Dec-07 09:53 UTC
Re: Desperate Newbie Question: Going round in circles trying to save a HABTM
On Dec 6, 2005, at 10:33 PM, Eric Sloane wrote:> I have a couple of tables, Jobs and Addresses with a HABTM > relationship through Jobs_AddressesJoin table names should be in alphabetical order: addresses_jobs instead of jobs_addresses. Table names should also be lowercase. -- _Deirdre http://deirdre.net
Eric Sloane
2005-Dec-07 11:55 UTC
Re: Desperate Newbie Question: Going round in circles trying to save a HABTM
Peter Donald wrote:> On 12/7/05, Eric Sloane <esloane-9MXrTL7Q3obvnOemgxGiVw@public.gmane.org> wrote: > >>Hmm Peter, OK - thanks for your really helpful response!! Next time - at >>the very least - you could perhaps post a quote of the original question >>along with your critisism. That way your input could be evaluated beside >>the problem! > > > Easy tiger. > > I was only trying to help you get an answer to your question. Specifically I > asked for you to provide more information so that I could help you. > Specifically "a stack trace, version of rails, relevent snippets of the model > code" none of which you seem to have provided. Others may be able to help > you with your problem with what you have provided but I certainly can not. > > As a side note you may want to have a read through "How To Ask Questions > The Smart Way" [1] if you want to get more useful response. > > [1] http://www.catb.org/~esr/faqs/smart-questions.html > > -- > Cheers, > > Peter Donald > > Blog: http://www.RealityForge.orgOK Peter - we''ve got off on the wrong foot it seems - I have no intention of causing offence. I thought I had included enough information since it''s a trivial newbie question regarding a lack of understanding of HABTM, ActiveRecord, and, specifically how to code a save in a controller on the the three tables, i.e. jobs, addresses and jobs_addresses (which is specified as a join_table in the model) I''m using ruby 1.8.2, rails 0.14.3 if that is of any use. The issue is >I have a couple of tables, Jobs and Addresses with a HABTM relationship >through Jobs_Addresses and I have a model and controller for both jobs >and addresses. What would be the best way to code a create method to >save the two data sets? I tried; > > def create > @job = Job.new(params[:job]) > @address = Address.new(params[:address]) > if @job.save && @address.save > flash[:notice] = ''Job was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > end >Which resulted in; > NoMethodError in Job#create >undefined method `address'' for #<Job:0x3912190> >Request >Parameters: {"commit"=>"Save Job Data", "job"=>{"job_number"=>"1984", >"closed"=>"false", "job_phase_id"=>"1", "job_type_id"=>"1"}, >"address"=>{"country"=>"Australia", "suburb"=>"Spanish Harlem", >"address_1"=>"Level 42", "address_2"=>"77, Sunset Strip", >"state"=>"Confused"}} >So all the data is there I just don''t know how to split the save to two >tables. Where my problem - or lack of understanding - lies, is specifically within the controller. The models state; class Job < ActiveRecord::Base has_and_belongs_to_many :people has_many :milestones has_many :job_phases has_many :people has_and_belongs_to_many :addresses, :join_table => "jobs_addresses" has_many :reminders has_many :discussions has_many :actions has_one :jobtype has_many :documents has_and_belongs_to_many :organisations validates_associated :address end class Address < ActiveRecord::Base has_one :address_type has_and_belongs_to_many :jobs, :join_table => "jobs_addresses" has_one :organisation has_many :people end As to the stack trace - well I''ve stated the results and the request paramenters - what more do you need? As to relationships outside of Jobs/Addresses, I''m not concerned with those right now, getting over this fundamentally dumb(on my part) lack of understanding will keep me going for a while ;~) Hope this is enough info for you - and thanks, truly Kind Regards, Eric
Eric Sloane
2005-Dec-07 11:59 UTC
Re: Desperate Newbie Question: Going round in circles trying to save a HABTM
Deirdre Saoirse Moen wrote:> On Dec 6, 2005, at 10:33 PM, Eric Sloane wrote: > >> I have a couple of tables, Jobs and Addresses with a HABTM >> relationship through Jobs_Addresses > > > Join table names should be in alphabetical order: addresses_jobs > instead of jobs_addresses. > > Table names should also be lowercase. >Thanks Dierdre table names *are* lowercase and the join table is specifically set in the model. It seems I was *indeed* too vague in my presentation of the problem. My apologies. Kind Regards, Eric
Peter Donald
2005-Dec-07 12:37 UTC
Re: Re: Desperate Newbie Question: Going round in circles trying to save a HABTM
On 12/7/05, Eric Sloane <esloane-9MXrTL7Q3obvnOemgxGiVw@public.gmane.org> wrote:> OK Peter - we''ve got off on the wrong foot it seems -no problem> >Which resulted in; > > > NoMethodError in Job#create > > >undefined method `address'' for #<Job:0x3912190>> As to the stack trace - well I''ve stated the results and the request > paramenters - what more do you need?If you run the application in development mode, when it crashes you should get a full trace link that you can click on and it will give you a stack trace with all the methods and line numbers called up the stack. IIRC it is just before request parameters are displayed. I can not figure out where Job.create is being called from your description thus having a tough time figuring out what is wrong. Feel free to email it to me off list and I will try and help some more. -- Cheers, Peter Donald Blog: http://www.RealityForge.org
Kent Sibilev
2005-Dec-07 13:42 UTC
Re: Re: Desperate Newbie Question: Going round in circles trying to save a HABTM
In you Job model I noticed you have two different associations to the same people table. You can''t do that. At least call them with different names. Kent. On Wednesday 07 December 2005 06:55, Eric Sloane wrote:> The models state; > class Job < ActiveRecord::Base > has_and_belongs_to_many :people > has_many :milestones > has_many :job_phases > has_many :people > has_and_belongs_to_many :addresses, :join_table => "jobs_addresses" > has_many :reminders > has_many :discussions > has_many :actions > has_one :jobtype > has_many :documents > has_and_belongs_to_many :organisations > validates_associated :address > > end > class Address < ActiveRecord::Base > has_one :address_type > has_and_belongs_to_many :jobs, :join_table => "jobs_addresses" > has_one :organisation > has_many :people > end >
Justin Forder
2005-Dec-08 02:44 UTC
Re: Re: Desperate Newbie Question: Going round in circles trying to save a HABTM
Kent Sibilev wrote:> In you Job model I noticed you have two different associations to the same > people table. You can''t do that. At least call them with different names.Kent is right. Now the error Rails reported was undefined method `address'' for #<Job:0x3912190> and you specify validates_associated :address so Rails will be trying to access an associated object via the address method in order to validate it. However, your model includes has_and_belongs_to_many :addresses, :join_table => "jobs_addresses" so you should be using validates_associated :addresses See the Rails documentation: "validates_associated(*attr_names) Validates whether the associated object or objects are all valid themselves. Works with any kind of association. class Book < ActiveRecord::Base has_many :pages belongs_to :library validates_associated :pages, :library end" By the way, it would have helped if you had given your table definitions. It looks as if you have created something very complex before starting to test it. This gives you lots of uncertainty about where the errors are. Read up on test-driven development. regards Justin> > Kent. > > On Wednesday 07 December 2005 06:55, Eric Sloane wrote: > >>The models state; >>class Job < ActiveRecord::Base >> has_and_belongs_to_many :people >> has_many :milestones >> has_many :job_phases >> has_many :people >> has_and_belongs_to_many :addresses, :join_table => "jobs_addresses" >> has_many :reminders >> has_many :discussions >> has_many :actions >> has_one :jobtype >> has_many :documents >> has_and_belongs_to_many :organisations >> validates_associated :address >> >>end >>class Address < ActiveRecord::Base >> has_one :address_type >> has_and_belongs_to_many :jobs, :join_table => "jobs_addresses" >> has_one :organisation >> has_many :people >>end >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Justin Forder
2005-Dec-08 02:52 UTC
Re: Re: Desperate Newbie Question: Going round in circles trying to save a HABTM
Eric Sloane wrote:> Deirdre Saoirse Moen wrote: > >> On Dec 6, 2005, at 10:33 PM, Eric Sloane wrote: >> >>> I have a couple of tables, Jobs and Addresses with a HABTM >>> relationship through Jobs_Addresses >> >> >> >> Join table names should be in alphabetical order: addresses_jobs >> instead of jobs_addresses. >> >> Table names should also be lowercase. >> > Thanks Dierdre > table names *are* lowercase and the join table is specifically set in > the model.If you followed the Rails convention, as Deirdre advised, you wouldn''t have to set the join table in the model.> It seems I was *indeed* too vague in my presentation of the > problem. My apologies.Not vague in this case, actively misleading :-)> Kind Regards, > Eric > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >
Eric Sloane
2005-Dec-08 03:13 UTC
Re: Desperate Newbie Question: Going round in circles trying to save a HABTM
Hi All, Thanks to everyone who responded to my questions. Kent - I hadn''t noticed the dual declaration (was too focussed elsewhere). Justin, yeah - that plural/singular thing still has me foxed with regard to when to use what. Peter and Ken, I''m working with your solutions today. To everyone else - thanks again, Kind Regards, Eric. Justin Forder wrote:> Kent Sibilev wrote: > >> In you Job model I noticed you have two different associations to the >> same people table. You can''t do that. At least call them with >> different names. > > > Kent is right. > > Now the error Rails reported was > > undefined method `address'' for #<Job:0x3912190> > > and you specify > > validates_associated :address > > so Rails will be trying to access an associated object via the address > method in order to validate it. However, your model includes > > has_and_belongs_to_many :addresses, :join_table => "jobs_addresses" > > so you should be using > > validates_associated :addresses > > See the Rails documentation: > > "validates_associated(*attr_names) > > Validates whether the associated object or objects are all valid > themselves. Works with any kind of association. > > class Book < ActiveRecord::Base > has_many :pages > belongs_to :library > > validates_associated :pages, :library > end" > > By the way, it would have helped if you had given your table definitions. > > It looks as if you have created something very complex before starting > to test it. This gives you lots of uncertainty about where the errors > are. Read up on test-driven development. > > regards > > Justin > >> >> Kent. >> >> On Wednesday 07 December 2005 06:55, Eric Sloane wrote: >> >>> The models state; >>> class Job < ActiveRecord::Base >>> has_and_belongs_to_many :people >>> has_many :milestones >>> has_many :job_phases >>> has_many :people >>> has_and_belongs_to_many :addresses, :join_table => >>> "jobs_addresses" >>> has_many :reminders >>> has_many :discussions >>> has_many :actions >>> has_one :jobtype >>> has_many :documents >>> has_and_belongs_to_many :organisations >>> validates_associated :address >>> >>> end >>> class Address < ActiveRecord::Base >>> has_one :address_type >>> has_and_belongs_to_many :jobs, :join_table => "jobs_addresses" >>> has_one :organisation >>> has_many :people >>> end >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >>