byroot / activerecord-typedstore

ActiveRecord::Store but with type definition
MIT License
441 stars 57 forks source link

Can't query columns #82

Closed ManuelFrigerio closed 4 years ago

ManuelFrigerio commented 4 years ago

Hi,

I have the following column in my User table:

typed_store :onboarding_questions, coder: ActiveRecord::TypedStore::IdentityCoder do |s|
    s.string :esp
    s.string :signup_source
    s.string :newsletter_list_size
    s.string :main_challenge
    s.string :newsletter_monetization
  end

The onboarding_questions column is of type JSON.

However, when I try to query the User table

User.where(esp: "convertkit").count

I get an error:

ActiveRecord::StatementInvalid (PG::UndefinedColumn: ERROR:  column users.esp does not exist
LINE 1: SELECT COUNT(*) FROM "users" WHERE "users"."esp" = $1
                                           ^
)

What am I doing wrong?

Thanks

casperisfine commented 4 years ago

That's a PostgreSQL / Active Record question, it doesn't have much to do with typedstore. To query JSON field you need to use the Postgres specific syntax, e.g. first Google result: https://makandracards.com/makandra/37851-how-to-query-postgresql-s-json-fields-from-rails