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

  • Home
  • SEARCH
  • 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 8148433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:38:44+00:00 2026-06-06T14:38:44+00:00

In Rails 3.2 I’m trying to set a default OdontologicUnity instace when a User

  • 0

In Rails 3.2 I’m trying to set a default OdontologicUnity instace when a User is created.

User:

require "odontological_unity"
class User < ActiveRecord::Base

    attr_accessible :odontologic_unity, ...
    ...

    belongs_to :odontologic_unity

    after_create :associate_default_unity

    private 
    def associate_default_unity
      ou = OdontologicUnity::DEFAULT
      ou.save!
      self.update_attribute :odontologic_unity, ou
    end

end

OdontologicUnity:

# encoding: UTF-8
class OdontologicUnity < ActiveRecord::Base
  ...

  DEFAULT = OdontologicUnity.new(
    name: 'Actualiza el nombre',
    address: 'Actualiza la dirección',
    nit: 'Actualiza el NIT',
    mission: "Actualiza la misión",
    vision: "Actualiza la visión"
  )
end

The code works without raising exceptions but when I see to my DB I got this (odontological_unity_id: nil):

User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 9]]
=> #<User id: 9, email: "user2@example.com", password_digest: "$2a$10$BPDmM6stW0sO6tQbwwJCxuWO4hvWIAeSzogEl2GGiqmo...", remember_token: "ltU5RBcifDsr9uPIci9z_A", names: "Example User", lastnames: "Some Lastnames", identity_document_number: "1515", odontological_unity_id: nil, created_at: "2012-06-24 15:00:17", updated_at: "2012-06-24 15:02:05">

The weird thing about it is the OdontologicUnity record is actually created, but doesn’t get associated with the user.

  • 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-06T14:38:45+00:00Added an answer on June 6, 2026 at 2:38 pm

    I would recommend you use a method for creating a default user, not a constant. Regarding your problem, try a before create instead and assign the id:

    before_create :associate_default_unity
    
    def associate_default_unity
      ou = OdontologicUnity::DEFAULT
      ou.save!
      self.write_attribute :odontologic_unity_id, ou.id
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Rails 3.0.4 Given the following relationship class Child < ActiveRecord::Base belongs_to :parent end class
Rails question. The default behavior for error validation is to fieldWithError-styled div around the
Rails: I need to set a condition in a has_many that needs the self.id
Rails relies on: actionmailer = 3.2.0 actionpack = 3.2.0 activerecord = 3.2.0 activeresource =
rails 2.3.4, sqlite3 I'm trying this Production.find(:all, :conditions => ["time > ?", start_time.utc], :order
Rails 3.2.1 app name: demo database: mongoDB with mongoid I have this scaffolding set
Rails WEBrick shows raw SQL statements for any ActiveRecord activities. How to enable that
Rails 3.1, Ruby 1.9.2 I am trying to adapt Railscasts episode 345 (HSTORE) to
Rails 2.3.5 For a link_to image_tag', I've been trying to figure out how to
Rails model: Categories have items Trying to get a list of categories that actually

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.