Forum: Ruby on Rails Saving a User Object while in the Address Controller

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.
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-02-03 08:49
(Received via mailing list)
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 Goodwin
ericgoodwin.com
311849893e0a9c683419fd77e6c52e7e?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-02-03 08:55
(Received via mailing list)
Have you defined any realations between user and address???

just do user.adress =@address

Mikkel
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-02-03 09:04
(Received via mailing list)
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 Bruun wrote:

>     Hi,
>
>     In my user class I have attr_accessible  :address_id so I should
>     _______________________________________________
>http://lists.rubyonrails.org/mailman/listinfo/rails
>
>


--
Eric Goodwin
ericgoodwin.com
311849893e0a9c683419fd77e6c52e7e?d=identicon&s=25 Mikkel Bruun (Guest)
on 2006-02-03 09:10
(Received via mailing list)
did the address get saved???

Mikkel
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-02-03 09:16
(Received via mailing list)
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 Bruun 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
>     >
>     >     Rails@lists.rubyonrails.org
>     >Rails@lists.rubyonrails.org <mailto:Rails@lists.rubyonrails.org>
>     Rails mailing list
>
>


--
Eric Goodwin
ericgoodwin.com
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-02-03 11:43
(Received via mailing list)
On 2/3/06, Eric Goodwin <ruby@ericgoodwin.com> 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.
Ac1da5d66d51a1c3b843221032247b30?d=identicon&s=25 Eric Goodwin (Guest)
on 2006-02-04 19:47
(Received via mailing list)
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 Dyk 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
>>>    >
>>>    >     Rails@lists.rubyonrails.org
>>>    >Rails@lists.rubyonrails.org <mailto:Rails@lists.rubyonrails.org>
>>>    Rails mailing list
>>>
>>http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>>
>>
>_______________________________________________
>Rails mailing list
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails
>
>


--
Eric Goodwin
ericgoodwin.com
This topic is locked and can not be replied to.