The set up is ruby on rails, in a postgres database. The table is called line_sources and a JSON column is called names. I want to return all rows where the names column contains a value called name1. I'm trying this but it fails:
LineSource.where("names ->> '%' = 'name1'")
Perhaps jsonb can be used for that?
Related
I have an array of students with a jsonb field attendance array. I wanted to add a new object into it for all volunteers. How can i do this. I'm using ruby on rails and postgres.
I tried jsonb updateall according to the documentation. And tried this
Item.where(id: items).
update_all(
"properties = jsonb_set(properties, '{price}', to_json(#{unique_price}::int)::jsonb)"
)
I having a rails application, with database as postgressql.
I am having a requirement, where i need to share the datatype of the field as per postgres sql database terminology for the sake of API integration.
Is there any way or method by which we can get the native data types of PostgreSQL database of my application.
i tried the below commands in rails console
fields_array ["abc","cde","mno","pqr","stv" ,,,,,,, so on]
fields_array.each do |field_name|
p field_type = User.type_for_attribute(field_name).type
p sql_type = User.type_for_attribute(field_name).sql_type ## not working###
end
But i am getting the datatype as per rails. i need the datatype of the column and size as per the postgressql native datatype. as there are hundredes of fields, i want to loop through them, and get the datatype of each field.
sql_type = User.columns.map{ |column| column.sql_type if c.name == field_name }.compact.first
You can get all the column sql_types in the User.columns collection. You iterate with map over them and return the column.sql_type if its name is equal to the field_name. This returns an array with many nils and one sql_type string if the field_name exists. .compact then just clears the array from nils and returns a single element array. Now you can call .first to get the plain string.
I’m using Rails 4.2.7. I have an array of my model objects and currently I’m iterating through that array to find matching entries in the database based on a field my each object …
my_object_times.each_with_index do |my_object_time, index|
found_my_object_time = MyObjectTime.find_by_my_object_id_and_overall_rank(my_object_id, my_object_time.overall_rank)
My question is, how can I rewrite the above to run one query instead of N queries, if N is the size of the array. What I wanted was to force my underlying database (PostGres 9.5) to do a “IF VALUE IN (…)” type of query but I’m not sure how to extract all the attributes from my array and then pass them in appropriately to a query.
I would do something like this:
found_my_object_times = MyObjectTime.where(
object_id: my_object_id,
overall_rank: my_object_times.map(&:overall_rank)
)
My postgres json data looks like this:
changes: {"data"=>[nil, {"margin"=>0.0, "target"=>0.77777}]}
The field name is changes and it's a postgres json data type.
How do I write an active record query to check for rows where data has a key named margin?
For example, the first record would be included while the second would not:
# record 1
changes: {"data"=>[nil, {"margin"=>0.0, "target"=>0.77777}]}
# record 2
changes: {"data"=>[nil, {"foo"=>0.0, "target"=>0.77777}]}
I've tried something like the following but it's not working:
ModelName.where("changes -> 'data' ?| array[:keys]", keys: ['margin'])
Okay, this is what I found
Query based on JSON document
The -> operator returns the original JSON type (which might be an object), whereas ->> returns text
Also, this post is probably what you look for.
Maybe try something like
ModelName.where("changes->>'margin' > -1")
The answer:
ModelName.where("changes -> 'data' -> 1 ? 'margin'")
The query logic is something like...in the changes field, move to object data, get second element (zero based index), see if it contains a key named margin.
The set up is ruby on rails, in a postgres database. The table is called line_sources and a JSON column is called names. I want to return all rows where the names column contains a key called away_names. I'm trying this but they fail:
LineSource.where("names -> 'away_names'")
and
LineSource.where("names ->> 'away_names' = '%'")
Try this :
where("(names->'away_names') is not null")
You can use #> to get the JSON object at that path.
where("(names #>'{away_names}') is not null")
Basic key operators to query the JSON objects :
#> : Get the JSON object at that path
->> : Get the JSON object at that path as text
{obj, n} : Get the nth item in that object