Reputation: 57
I'm trying to select values from a jsonb column, payload
. The json has a format like
{"id" : 123456, "email": "[email protected]", "items" : [{}, {}, ...]}
Using ActiveRecord, I'm trying ModelRecord.select("payload ->> 'email'").all and all the records are being returned #<ModelRecord id: nil>
.
I'm sure I'm doing something wrong or I'm misunderstanding the correct use.
PostgreSQL 9.6.5 on x86_64-pc-linux-gnu Rails 5.1.3
Upvotes: 0
Views: 110
Reputation: 44370
You're misunderstanding the correct use.
I'm trying
ModelRecord.select("payload ->> 'email'")
and all the records are being returned[#<ModelRecord id: nil>, ... ]
This is how AR works, AR maps columns to the model attributes, payload
is a jsonb column and for that reason isn't mapped properly. Your records are valid and you can access select
-ed attribute in a plain way, try it:
col = ModelRecord.select("payload ->> 'email'")
col.first.payload
Upvotes: 1
Reputation: 13
I'm not sure if I understood your question, but I think you are trying to get the only the email column if it is, you can do this.
def index
@emails = Model.select(:email)
end
hope it helps.
Upvotes: 0