Saving a User Object while in the Address Controller


#1

Hi,
My saves are failing me and I can’t figure out why.
I am trying to save an address id to a user object just after I create
the address.
This isn’t the exact code, but it shows what I am trying to do.

class AddressesController < ApplicationController
def create
@address = Address.new(params[:address])
saved_address = @address.save

    @user = User.find(params[:id])
    @user.address_id = @address.id
    saved_user = @user.save
end

end

User.save keeps on returning false.
Any ideas why? I can access different attributes of the user, so I know
that the proper user is getting returned from the find.
In my user class I have attr_accessible :address_id so I should be able
to access it, shouldn’t I?

Cheers,
Eric


Eric G.
ericgoodwin.com


#2

Have you defined any realations between user and address???

just do user.adress =@address

Mikkel


#3

Hi Mikkel,
I already has the relationships defined but I tried your code as well.
It still won’t save. It’s quite bizarre.
Thanks for the try.
Eric

Mikkel B. wrote:

Hi,

In my user class I have attr_accessible  :address_id so I should
_______________________________________________

http://lists.rubyonrails.org/mailman/listinfo/rails


Eric G.
ericgoodwin.com


#4

did the address get saved???

Mikkel


#5

No. I think it must be something to do with my User class. I have tried
the same code but with a different class and it works.
I am going to have a deeper look into my user class now and see if I can
figure out why it won’t let itself be updated.
Eric

Mikkel B. wrote:

It still won't save. It's quite bizarre.
>
>     This isn't the exact code, but it shows what I am trying to do.
>        end
>
>     removed_email_address@domain.invalid
>removed_email_address@domain.invalid <mailto:removed_email_address@domain.invalid>
Rails mailing list


Eric G.
ericgoodwin.com


#6

Thanks Joe,
I ended up having a message in my errors due to a poorly written
validate statement.
Thanks for the help.
Eric

Joe Van D. wrote:

It still won’t save. It’s quite bizarre.

This isn't the exact code, but it shows what I am trying to do.
   end

removed_email_address@domain.invalid

removed_email_address@domain.invalid mailto:removed_email_address@domain.invalid
Rails mailing list

http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Eric G.
ericgoodwin.com


#7

On 2/3/06, Eric G. removed_email_address@domain.invalid wrote:

No. I think it must be something to do with my User class. I have tried
the same code but with a different class and it works.
I am going to have a deeper look into my user class now and see if I can
figure out why it won’t let itself be updated.
Eric

If the user class has any validations or anything that fail, then the
save of the user object will fail.

Try looking at what user.errors.full_messages is after the save.