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 8353683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:19:56+00:00 2026-06-09T09:19:56+00:00

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

  • 0

My application has Users and Dwellings. When a user creates a dwelling, the user becomes the owner of the dwelling through a rails association. Through my create method, the user_id is successfully assigned to the newly created dwelling in the owner_id column, however the dwelling_id is not propagated to the dwelling_id in the user‘s record. I’m not sure if it is the relationship or method incorrectly set up, but the problem seems to occur when an attempt is made to save the user, as the dwelling is successfully created. Below are my models and method implementations.

Dwelling Model

# 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"

  validates :street_address, presence: true
  validates :city, presence: true
  validates :state, presence: true
  validates :zip, presence: true

end

—

User Model

# user.rb
class User < ActiveRecord::Base
  attr_accessible :email, :first_name, :last_name, :password, :password_confirmation, :zip
  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"
...

—

Dwellings Controller (create method)

# 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

—

As mentioned above, the dwelling is successfully created but the “…but something prevented us from adding you as a rookie…” flash is triggered from the else conditional.

Update 8/3/12 9:11PM EST

I updated the create action to use user.save! to force the transaction. The following error was output. Apparently the password is required somehow.

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
  • 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-09T09:19:58+00:00Added an answer on June 9, 2026 at 9:19 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails application that has three different types of users and I
I have the following models: class Application(models.Model): users = models.ManyToManyField(User, through='Permission') folder = models.ForeignKey(Folder)
I have a web application which has several users and each user will have
An application has a MySQL database containing a table of Users. Each User has
Take for example an application which has users, each of which can be in
Hi Sitepoint wizard people, Say we have an admin application that has multiple users
My current backbone application has a url: localhost/#users Is there a way to access
Problem Overview: My application has an enrollment form. Users have a habit of entering
I am currently working on an application that has different permissions/users for the local
Say my application has a list of items of some kind, and users can

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.