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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:24:57+00:00 2026-05-26T22:24:57+00:00

Using Rails 3 and ActiveModel, I am unable to use the self. syntax to

  • 0

Using Rails 3 and ActiveModel, I am unable to use the self. syntax to get the value of an attribute inside an ActiveModel based object.

In the following code, in save method, self.first_name evaluates to nil where @attributes[:first_name] evaluates to ‘Firstname’ (the value passed in from the controller when initializing the object).

In ActiveRecord this seems to work, but when building the same class in ActiveModel, it does not. How do you refer to a field using accessors in an ActiveModel based class?

class Card
  include ActiveModel::Validations
  extend ActiveModel::Naming 
  include ActiveModel::Conversion
  include ActiveModel::Serialization
  include ActiveModel::Serializers::Xml

  validates_presence_of :first_name

  def initialize(attributes = {})
    @attributes = attributes
  end

  #DWT TODO we need to make sure that the attributes initialize the accessors properyl, and in the same way they would if this was ActiveRecord
  attr_accessor :attributes, :first_name

  def read_attribute_for_validation(key)
    @attributes[key]
  end

  #save to the web service
  def save
    Rails.logger.info "self vs attribute:\n\t#{self.first_name}\t#{@attributes["first_name"]}"
  end

  ...

end
  • 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-26T22:24:58+00:00Added an answer on May 26, 2026 at 10:24 pm

    I figured it out. The “hack” that I mentioned as a comment to Marian’s answer actually turns out to be exactly how the accessors for ActiveRecord classes are generated. Here’s what I did:

    class MyModel
      include ActiveModel::AttributeMethods
    
      attribute_method_suffix  "="  # attr_writers
      attribute_method_suffix  ""   # attr_readers
    
      define_attribute_methods [:foo, :bar]
    
      # ActiveModel expects attributes to be stored in @attributes as a hash
      attr_reader :attributes
    
      private
    
      # simulate attribute writers from method_missing
      def attribute=(attr, value)
        @attributes[attr] = value
      end
    
      # simulate attribute readers from method_missing
      def attribute(attr)
        @attributes[attr]
      end
    end
    

    You can see the same thing if you look in ActiveRecord’s source code (lib/active_record/attribute_methods/{read,write}.rb).

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

Sidebar

Related Questions

Using Rails 3.2.0.rc2 and ruby 1.9.3p0 In app/views/requests/_form.html.erb I have the following code for
I'm using Rails 3 Beta and I assume the syntax is similar to the
I'm using Rails 3 w/ Mongoid, (so no ActiveRecord). Mongoid uses ActiveModel's to_json method,
I'm trying to use the rails plugin with hudson, but i'm getting the following
I'm following Railstcast #188 to get declarative authorization running on my Rails 3 app
I am using Rails 3.0.10 class Friend attr_accessor :first_name, :last_name, :email extend ActiveModel::Naming include
I have a Rails 3.1.1 Project using the Gem ActiveFrom an extension of ActiveModel
Using Rails v2.1, lets say you have an action for a controller that is
When using Rails date_select with :prompt => true I see some very strange behavior
I'm using rails, but doing all my Javascript unobtrusively using jQuery in application.js .

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.