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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:28:30+00:00 2026-05-12T13:28:30+00:00

I was bitten by this recently, and it’d be useful to know precisely what’s

  • 0

I was bitten by this recently, and it’d be useful to know precisely what’s happening to make this happen, so others avoid this mistake.

I have a model User, with a schema like so:

create_table "users", :force => true do |t|
    t.string   "user_name"
    t.string   "first_name"
    t.string   "last_name"
    t.string   "email"
    t.string   "location"
    t.string   "town"
    t.string   "country"
    t.string   "postcode"
    t.boolean  "newsletter"

In the class user.rb, I have a attr_accessor for three methods:

class User < ActiveRecord::Base

# lots of code

  attr_protected :admin, :active

# relevant accessor methods

  attr_accessor :town, :postcode, :country 

end

Now in my user controller, if I have the following method:

def create
    @user = User.new params[:user]
end

When when I try to create a new user with the contents in this params hash:

  --- !map:HashWithIndifferentAccess 
  # other values
  country: United Kingdom
  dob(1i): "1985"
  dob(2i): "9"
  dob(3i): "19"
  town: london

The returned object has empty strings for the country, town and postcode postcode values, like so.

(rdb:53) y user1
--- !ruby/object:User 
attributes: 
  # lots of attributes that aren't relevant for this example, and are filled in okay
  postcode: 
  country: 
  town: 

I can tell that the attr_accessor methods are clobbering Active Record’s existing accessor methods, because when I take them out all works fine, so the solution is fairly straightforward – just take them out.

But what exactly is happening when here?

I’m looking here in the Rails API docs for Active Record, and here in Ruby’s own docs about attr_accessor, but I’m still slightly hazy about how attr_accessor is breaking things here.

Any able to shed some light to stop some other poor soul falling foul of this?

  • 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-05-12T13:28:31+00:00Added an answer on May 12, 2026 at 1:28 pm

    When you add an attr_accessor to a class, it defines two methods on it, e.g. User#postcode and User#postcode=.

    If the name of the accessor is equal to a name of a model attribute, things break (if you’re not careful). When you assign attributes to the model, User#postcode= is called and in your case it does nothing except

    @postcode = value
    

    So the value just gets stored in an instance variable and doesn’t appear in the attributes hash.

    Whereas in a normal scenario (without an accessor) this would go to method_missing and eventually trigger something like

    write_attribute(:postcode, value)
    

    And then it would appear in your model’s attributes.
    Hope that makes sense.

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

Sidebar

Related Questions

I have been bitten by something unexpected recently. I wanted to make something like
I've recently been bitten by the javascript:void(null); bug in my hrefs when applied to
I've recently been bitten by the (way too commmon in my opinion) gotcha of
A colleague of mine recently got bitten badly by writing out of bounds to
I was recently working with a DateTime object, and wrote something like this: DateTime
Encoding issues are among the one topic that have bitten me most often during
I am bitten by this little inconsistent debugger behavior. The quotemeta() function seems to
Why on Earth doesn't the interpreter raise SyntaxError everytime I do this: my_abc =
I would like to start my mysql server with the --external-locking option. As mysqld

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.