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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:43:09+00:00 2026-05-22T17:43:09+00:00

I have a City model with a cached_info field which is a serialized hash.

  • 0

I have a City model with a cached_info field which is a serialized hash.

{:population=>20000, :more_stuff =>....}

If I make the following query in Activerecord.

City.where('cached_info[:population] > 300').count

I get back…

ActiveRecord::StatementInvalid: Mysql2::Error: 
You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the right syntax to use near '[:population] > 300)' at line 1: SELECT COUNT(*) FROM `places` WHERE `places`.`type` = 'City' AND (cached_info[:population] > 3)

Anybody have a workaround for 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-22T17:43:10+00:00Added an answer on May 22, 2026 at 5:43 pm

    There’s no easy way to query within a serialised Hash via ActiveRecord and SQL, unless you use a LIKE in your query (but this can’t do comparisons like > and <).

    Based on your use case you should really rethink your data model and normalise these fields into proper models/columns.

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

Sidebar

Related Questions

I have the following models: class City(models.Model): name = models.CharField(max_length=100) class Pizza(models.Model): name =
I have this model: class Location < ActiveRecord::Base acts_as_gmappable def gmaps4rails_address #{self.city}, #{self.country} end
I have a codefirst entity model which has two entities, City and Locality. City
I have the following model structure Country, City(fk=country) and School(fk=city). Now I want to
Suppose you have two models, User and City, joined by a third model CityPermission:
I have a city model and a wood_production model. A city has one wood_production
I have such model: class Place(models.Model): name = models.CharField(max_length=80, db_index=True) city = models.ForeignKey(City) address
So I have a state model and city model associated with has_many and belongs_to.
So I have a few Django models that look like this: class City(models.Model): name
I have this domain model: class Person < ActiveRecord::Base composed_of :address, mapping: [%w(address_street street),

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.