0

I have a page that takes a user through a short sign up tutorial when they create their account in order to create their first resource. In my app, :hotel belongs to :user, and :user has_one hotel. For the tutorial page, in my controller, I have:

    @hotel = current_user.build_hotel

Which works, except that it a user somehow finds him back on the tutorial page that command disassociates their previously created hotel. In other words, the second time the user accesses the page with:

 @hotel = current_user.build_hotel

The user_id field in the hotel they created the first time becomes nil. Obviously that is a serious problem. I can do a before_filter on that page, but I'm not very happy about having a way for the user to screw everything up simply by visiting a page. How should I properly use the build command for a has_one relationship?

Tim Reistetter
  • 829
  • 1
  • 12
  • 17
  • I an't see the issue, I guess you've a hidden field containing the user id. This one could be set with `current_user` directly. – apneadiving Oct 02 '12 at 17:54

1 Answers1

2

You can test for the existence of a hotel before building it:

 @hotel = current_user.hotel || current_user.build_hotel
Erez Rabih
  • 15,562
  • 3
  • 47
  • 64