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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:34:45+00:00 2026-05-15T13:34:45+00:00

I have a field in my Rails model that has max length 255. I’m

  • 0

I have a field in my Rails model that has max length 255.

I’m importing data into it, and some times the imported data has a length > 255. I’m willing to simply chop it off so that I end up with the largest possible valid string that fits.

I originally tried to do field[0,255] in order to get this, but this will actually chop trailing Unicode right through a character. When I then go to save this into the database, it throws an error telling me I have an invalid character due to the character that’s been halved or quartered.

What’s the recommended way to chop off Unicode characters to get them to fit in my space, without chopping up individual characters?

  • 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-15T13:34:46+00:00Added an answer on May 15, 2026 at 1:34 pm

    Uh. Seems like truncate and friends like to play with chars, but not their little cousins bytes. Here’s a quick answer for your problem, but I don’t know if there’s a more straighforward and elegant question I mean answer

    def truncate_bytes(string, size)
      count = 0
      string.chars.take_while{|c| (a += c.bytes.to_a.length) <= size }.join
    end
    

    Give a look at the Chars class of ActiveSupport.

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

Sidebar

Related Questions

In rails 3, I have a database model that has a birthday field, of
I have an active record Song model with a songhash field (string(255)) that contains
I'm on Rails 3. I have a model Client that has an address_id .
I have a Model called statistics which has a value field that contains Goals
Currently I have a model 'Locations' that has an image upload field added to
I have a Post model object that has reference to a parent object. The
I have got a Rails app running which displays data that is generated by
My overall use case: I have a Listing model that has many images .
In my Rails app, when creating a business I have a form that has
I have a model, Domain , which has a text field, names . >

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.