In Rails, how can I automatically update a User to own a newly created object of another class within the new object's Create method?

前端 未结 3 1839
旧巷少年郎
旧巷少年郎 2021-01-26 02:35

My application has Users and Dwellings. When a user creates a dwelling, the user becomes the owner

相关标签:
3条回答
  • 2021-01-26 03:14

    This was an issue with saving the user. I updated my create action in the Dwellings controller to to force a save of the user using user.save! to break the application and get an explicit error. Rails complained of a missing password (and password length) and password_confirmation. I am enforcing a presence validation on both password and password_confirmation as well as a length validation on password in my User model. I updated these validations to be enforced only on create. This solved the issue. With the validations no longer being enforced on user.save, the dwelling_id was successfully added to the user record. The newly implemented code is below.

    #dwellings_controller.rb
    ...
    def create
      @dwelling = current_user.properties.build(params[:dwelling])
    
      if @dwelling.save
        current_user.dwelling = @dwelling
        if current_user.save
          flash[:success] = "Woohoo! Your dwelling has been created. Welcome home!"
        else
          flash[:notice] = "You have successfully created a dwelling, but something prevented us from adding you as a roomie. Please email support so we can try to correct this for you."
        end
        redirect_to current_user
        else
          render 'new'
        end
      end
    ...
    

    --

    #user.rb
    ...
      validates :password, presence: { :on => :create }, length: { minimum: 6, :on => :create }
      validates :password_confirmation, presence: { :on => :create }
    
    0 讨论(0)
  • 2021-01-26 03:14

    The exact problem really depends on details of your application we haven't seen yet. For example, we don't know what current_user is defined as, we don't know how your users are created, we don't know any details about your forms, etc.

    I don't have much more time to work on this right now, but you might try the following things.

    1. First, remove has_secure_password from your User model and see if you still experience the issue. This is just for troubleshooting, as eventually you'll want to add has_secure_password back in.

    2. Make sure your users table has a password_digest column.

    3. Make sure the current_user has a value in the users table for password_digest Perhaps you created the user before you added the has_secure_password feature, which would result in a NULL value for password_digest.

    4. Make sure that current_user is actually a persisted (i.e. in the database) user. You can do this by adding the following line above current_user.save!:

      raise StandardError, "current_user is not persisted" unless current_user.persisted?
      
    5. I'm assuming current_user already exists as a persisted user. If you're actually doing something fancier like trying to create the user and the dwelling in the same form, then there are several other factors that need to be considered.

    I won't have additional time to help with this for a while. Best wishes getting it solved.

    0 讨论(0)
  • 2021-01-26 03:23

    UPDATE: Just to be clear, I misunderstood the user/dwelling relationship in the answer below, so this is not a solution to the problem.


    I think you're misunderstanding how associations work. With a belongs_to/has_many relationship, only the model that belongs to (in this case, Dwelling) has a foreign key pointing to the other record (in this case, User through the foreign key owner_id). The other record (User) does not need a key for every association, which would be redundant.

    Likewise, you don't need to assign @dwelling to current_user.dwelling, nor do you need to save the current_user. Just saving @dwelling after building it in current_user.properties.build(params[:dwelling]) should be enough.

    0 讨论(0)
提交回复
热议问题