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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:19:33+00:00 2026-06-15T16:19:33+00:00

I am following the Rails Tutorial doing a little project for myself and then

  • 0

I am following the Rails Tutorial doing a little project for myself and then try to progress.

Well I am facing a problem for which I found the solution, but I’ll really appreciate any thoughts and opinions because to make it work I have to disable a validation in an association.

The context is as follow : the application (big name for what it does actually…) has users to track their weight. I would like that when a user sign up, he/she enters a first measure on the go.

So here are the simplified models:

User.rb

class User < ActiveRecord::Base
  attr_accessible :email, :name, :password, :password_confirmation, :measures_attributes

  has_secure_password

  has_many :measures, dependent: :destroy
  accepts_nested_attributes_for :measures

  # here goes validations before_save, etc. taken from the Rails Tutorial
end

Measure.rb

class Measure < ActiveRecord::Base
  attr_accessible :weight
  belongs_to :user

  # This is kind where the problem is...
  # If I deactivate the validation for user_id everyhing goes fine
  validates :user_id, presence: true
  validates :weight, presence: true, numericality: { greater_than: 0 }

  default_scope order: 'measures.created_at ASC'
end

Here is the Users controller

class UsersController < ApplicationController

  # GET /users/new
  def new
    @user = User.new
    @user.measures.build
  end

  # POST /users
  def create

    @user = User.new(params[:user])

    if @user.save
      sign_in @user
      flash[:success] = "Hi #{@user.name}. Welcome !"
      redirect_to @user
    else
      render :new
    end
  end

end

Here is the form partial for the user :

<%= form_for(user) do |user_form| %>
  <%= render 'shared/error_messages', object: user_form.object %>

  <%= render 'users/fields', user_builder: user_form %>

  <%= user_form.fields_for :measures do |measure_fields| %>
    <%= render 'measures/fields', measure_builder: measure_fields, full: true %>
  <% end %>

  <%= user_form.submit submit_text, class: 'btn btn-large btn-primary' %>
<% end %>

And even when I fill the form correctly, I get this error :

* Measures user can't be blank

The only way I found to make it work is to get rid of the :user_id validation for presence in the Measure model. I want to emphasize that when this validation is off, the user is saved, the measure is saved and correctly associated with the newly created user.

Am I doing something wrong ? Is the :user_id presence validation in the Measure model really useful (it is in the Rails Tutorial and it makes perfectly sense for me) ? If yes why is the Measure validation failing when it is on ?

Thanks a lot in advance.

  • 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-15T16:19:35+00:00Added an answer on June 15, 2026 at 4:19 pm

    You could try validates :user instead of user_id. Then it might work out that the two are associated in memory even though the User hasn’t been saved yet.

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

Sidebar

Related Questions

Taking a rails tutorial, and I've run into the following problem that I'm having
I found the following table structures while I was watching ruby on rails tutorial.
I'm following the Kevin Skoglund tutorial Ruby on Rails 3 Essential Training, which was
Hey guys I'm following the rails tutorial found here http://net.tutsplus.com/tutorials/ruby/the-intro-to-rails-screencast-i-wish-i-had/ and I've gotten to
I'm doing a tutorial for rails animated loading page but the problem is that
I'm doing the Rails tutorial, and have been following it fairly strictly. However, my
I'm following this tutorial CSV-FILE-EXPORT-IMPORT-RAILS but something im doing wrong, because i got an
I'm I'm following the rails tutorial but I'm doing it on the server. Now,
I was doing an exercise from the ruby on rails tutorial which involves installing
I'm following the Rails Tutorial (Hartl), and having some trouble in section 3.6.4 Tests

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.