Forum: Ruby on Rails Desperate Newbie Question: Going round in circles trying to

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
esloane (Guest)
on 2005-12-07 08:35
(Received via mailing list)
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.j.donald (Guest)
on 2005-12-07 09:45
(Received via mailing list)
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 D.

Blog: http://www.RealityForge.org
esloane (Guest)
on 2005-12-07 11:22
(Received via mailing list)
Peter D. wrote:
> Peter D.
>
> Blog: http://www.RealityForge.org
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!
As you say - Cheers
peter.j.donald (Guest)
on 2005-12-07 11:31
(Received via mailing list)
On 12/7/05, Eric S. <removed_email_address@domain.invalid> 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 D.

Blog: http://www.RealityForge.org
deirdre (Guest)
on 2005-12-07 11:55
(Received via mailing list)
On Dec 6, 2005, at 10:33 PM, Eric S. 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.

--
_Deirdre                                             http://deirdre.net
esloane (Guest)
on 2005-12-07 14:02
(Received via mailing list)
Peter D. wrote:
> I was only trying to help you get an answer to your question. Specifically I
> --
> Cheers,
>
> Peter D.
>
> Blog: http://www.RealityForge.org
OK 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
esloane (Guest)
on 2005-12-07 14:14
(Received via mailing list)
Deirdre Saoirse M. wrote:
>
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.j.donald (Guest)
on 2005-12-07 14:38
(Received via mailing list)
On 12/7/05, Eric S. <removed_email_address@domain.invalid> 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 D.

Blog: http://www.RealityForge.org
ksibilev (Guest)
on 2005-12-07 15:44
(Received via mailing list)
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.
justin (Guest)
on 2005-12-08 04:44
(Received via mailing list)
Kent S. 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
justin (Guest)
on 2005-12-08 04:53
(Received via mailing list)
Eric S. wrote:
>> 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 :-)
esloane (Guest)
on 2005-12-08 05:21
(Received via mailing list)
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.
This topic is locked and can not be replied to.