Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:12:40+00:00 2026-06-09T15:12:40+00:00

I have a dwelling object that is built on a user . Dwellings Controller

  • 0

I have a dwelling object that is built on a user.

Dwellings Controller (create action)

# 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

The dwelling_id is not being saved to the current_user record. To get more information, I added the bang method (as shown above) to the current_user.save! command. Rails complains that the user‘s password is required to update the record, as shown below.

ActiveRecord::RecordInvalid in DwellingsController#create

Validation failed: Password can't be blank, Password is too short (minimum is 6 characters), Password confirmation can't be blank

If providing the user’s password through a hidden field isn’t the correct solution – and it does seem insecure – how can I correct this problem? Relevant sections of the user and dwelling model shown below.

#dwelling.rb
class Dwelling < ActiveRecord::Base
  attr_accessible :street_address, :city, :state, :zip, :nickname

  belongs_to :owner, :class_name => "User", :foreign_key => "owner_id"
  has_many :roomies, :class_name => "User"

#user.rb
class User < ActiveRecord::Base
  attr_accessible :email, :first_name, :last_name, :password, :password_confirmation, :zip, :dwelling_id
  has_secure_password

  before_save { |user| user.email = email.downcase }
  before_save :create_remember_token

  belongs_to :dwelling
  has_many :properties, :class_name => "Dwelling", :foreign_key => "owner_id"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:12:41+00:00Added an answer on June 9, 2026 at 3:12 pm

    Turns out the problem was stemming from my validations in the User model. The presence and length validations on password and password_confirmation were being imposed on user.save, which was failing as password and password_confirmation were not being provided. My update validations appear below.

    User Model

    #user.rb
    class User < ActiveRecord::Base
      attr_accessible :email, :first_name, :last_name, :password, :password_confirmation, :zip, :dwelling_id
      has_secure_password
    
      before_save { |user| user.email = email.downcase }
      before_create :create_remember_token
    
      belongs_to :dwelling
      has_many :properties, :class_name => "Dwelling", :foreign_key => "owner_id"
    
      validates :first_name, presence: true, length: { maximum: 50 }
      validates :last_name, presence: true, length: { maximum: 50 }
      VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
      validates :email, presence: true, 
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
      validates :password, presence: { :on => :create }, length: { minimum: 6, :on => :create }
      validates :password_confirmation, presence: { :on => :create }
    

    With this updated implementation, the user is successfully saved and the appropriate attributes are updated. This is a secondary issue stemming from this original question: 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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application has Users and Dwellings . When a user creates a dwelling ,
have written this little class, which generates a UUID every time an object of
I need to create a system comprising of 2 components: A single server that
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have a look at one of my websites: moskah.com The problem is that it
Have a simple contact us XPage created. Have server side validation in place that
I have dwelling on this for hours and still cannot find out how to
Have one Doubt In MVC Architecture we can able to pass data from Controller
Have a network location that shows paths in the 8.3 short format. I need
Have data that has this kind of structure. Will be in ascending order by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.