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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:38:07+00:00 2026-05-30T19:38:07+00:00

How Could I store integers (user id’s ranging from 1 to 9999) serialized in

  • 0

How Could I store integers (user id’s ranging from 1 to 9999) serialized in a database column and retrieve them back?

In my User model I have invites column,

User model
serialize: invites
invites = text field

Now I’m trying to do 2 things:

  • Append the user_id integer (from 1 to 9999) serialized in a column “invites”
  • Retrieve all the user id’s back from the User.invited column ( deserialize it ? )
  • 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-30T19:38:09+00:00Added an answer on May 30, 2026 at 7:38 pm

    From the fine manual:

    serialize(attr_name, class_name = Object)

    If you have an attribute that needs to be saved to the database as an object, and retrieved as the same object, then specify the name of that attribute using this method and it will be handled automatically. The serialization is done through YAML. If class_name is specified, the serialized object must be of that class on retrieval or SerializationTypeMismatch will be raised.

    So, if you want to store an array of integers as a serialized object, then:

    class User < ActiveRecord::Base
        serialize :invites, Array
        #...
    end
    

    You’d want the invites column to be a text column in the database (not string!) to avoid running into size issues.

    Then you can treat user.invites as a plain Array:

    user.invites = [ 1, 2, 3 ]
    user.invites.push(11)
    

    That of course doesn’t verify that that numbers are valid or that you don’t have duplicates (but you could use a Set instead of an Array for that), it also won’t prevent you from putting a string in there.

    I don’t recommend that you do this though, serialization is almost always a mistake that will come back to bite you later. A serialized column is an opaque blob of data as far as the database is concerned: you can’t update it in-place, you can’t query it, all you can do is pull it out of the database and put it back. serialize uses YAML for serialization and that’s an awful format if you need to work with your serialized data inside the database; you can also run into interesting encoding issues during upgrades.

    You’re better off setting up a traditional association table and a separate model (possibly using has_many ... :through =>) to handle this situation.

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

Sidebar

Related Questions

I'm trying to read a text file that contains integers and store them into
i wonder how i could store a xml structure in a persistence layer. cause
Sorry if this question is somewhat subjective. I am new to 'could store', 'distributed
Is there a good and free SVN repository I could use to store files
I'd like to store my json response in a global variable so, i could
I want to upload and store video files to my server using PHP. Could
I need a database that could be stored network drive and would allow multiple
I have fname and lname in my database, and a name could be stored
Could anyone point exactly where MSDN says thet every user stored procedure returns 0
I need an efficient data structure to store a list of integers. The amount

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.