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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:03:23+00:00 2026-05-30T11:03:23+00:00

class Post< ActiveRecord::Base end post_array = Post.first If I want to add some data

  • 0
 class Post< ActiveRecord::Base                                                                                                                                                                                                            
 end   

post_array = Post.first

If I want to add some data into p.

post_array['test'] = nil

this will make errors:

ActiveModel::MissingAttributeError: can't write unknown attribute \`ff'
        from ......rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.1/lib/active_record/attribute_methods/write.rb:34:in `write_attribute'

I think the reason is : this commit in github: Raise error when using write_attribute with a non-existent attribute

How can I insert some data into post_array , ie, post_array['test'] = nil?

Maybe there is some methods can convert this ActiveModel into hash or array?

  • 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-30T11:03:24+00:00Added an answer on May 30, 2026 at 11:03 am

    You can do this like so:

    post = Post.first
    hash = post.attributes
    hash['test'] = 'test'
    

    However you probably don’t want to: I imagine you’re struggling here with needing to store some data on an object, and models are all about storing data on themselves. If you want this data persisted to your datastore, you should write a migration that includes this column. If not, then you should use attr_accessor in your model:

    class Post < ActiveRecord::Base
      attr_accessor :test
    
    end
    
    post.test = 'test' # Now assigns 'test' to post correctly, and you can read it out the same way.
    

    Generally unless you’re converting the model’s data to a different format (like JSON or plist or something), changing it into a hash will usually just make your life more difficult.

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

Sidebar

Related Questions

class Followup < ActiveRecord::Base belongs_to :post belongs_to :comment end This model needs to only
Can someone explain me this Ruby on Rails puzzle? class Post < ActiveRecord::Base has_many
class Post < ActiveRecord::Base end post = Post.new How do I judge whether the
My env: ruby-1.9.2-preview3; rails-3.0.0.beta3 class PostFather < ActiveRecord::Base def self.inherited(subclass) end end class Post
class User < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base belongs_to :user end
assume we the following setup: class Post < ActiveRecord::Base belongs_to :user end class User
I have the following two models class Post < ActiveRecord::Base has_many :comments end class
If I have something like this: class Post < ActiveRecord::Base has_many :comments, :as =>
class Comment < ActiveRecord::Base belongs_to :post belongs_to :user end class Post < ActiveRecord::Base has_many
I have a typical, Post model: class Post< ActiveRecord::Base validates_presence_of :user_id #Line 1 validates_presence_of

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.