My search form submits multiple params with several values like in the example below.
I could create 1 single attribute for every one of those values like ethnicity_african, ethnicity_mixed, but this feels stupid. etc.
But is there a more simple way to store these 0 or 1 values ( selected or not selected ) into an active record model? Im using MySQL
{"european"=>"0", "african"=>"0", "middleeastern"=>"0", "indian"=>"0", "latinamerica"=>"0", "mixed"=>"0", "Asian"=>"0", "Caucasian"=>"0", "Pacific Islander"=>"0"}
UPDATE:
So serialization is the way to go.
It seems to be good enough for my need since I only store 0 or 1 values.
If you're using Postgres, you could look at using something like hstore to store the hash of data.
A tutorial on using hstore can be found on Railscasts.
Related
I've got a bigquery import from a firestore database where I want to query on a particular field from a document. This was populated via the firestore-bigquery extension and the document data is stored as a JSON string.
I'm trying to use a WHERE clause in my query that uses one of the fields from the JSON data. However this doesn't seem to work.
My query is as follows:
SELECT json_extract(data,'$.title') as title,p
FROM `table`
left join unnest(json_extract_array(data, '$.tags')) as p
where json_extract(data,'$.title') = 'technology'
data is the JSON object and title is an attribute of all of the items. The above query will run but yield 'no results' (There are definitely results there for the title in question as they appear in the table preview).
I've tried using WHERE title = 'technology' as well but this returns an error that title is an unrecognized field (hence the json_extract).
From my research this should work as a standard SQL JSON query but doesn't seem to work on Bigquery. Does anyone know of a way around this?
All I can think of is if I put the results in another table, but I don't know if that's a workable solution as the data is updated via the extension on an update, so I would need to constantly refresh my second table as well.
Edit
I'm wondering if configuring a view would help with this? Though ultimately I would like to query this based on different parameters and the docs here https://cloud.google.com/bigquery/docs/views suggest you can't reference query parameters in a view
I've since managed to work this out, and will share the solution for anyone else with the same problem.
The solution was to use JSON_VALUE in the WHERE clause instead e.g:
where JSON_VALUE(data,'$.title') = 'technology';
I'm still not sure if this is the best way to do this in terms of performance and cost so I will wait to see if anyone else leaves a better answer.
Is there a way to setup something like a <SelectInput> filter on a column of the list to get only distinct values of this column ?
Something like the <ReferenceInput> but on the same table and with unique values ...
No, but for good reason. Say you have data with billions of distinct records. You don't want your frontend determining what is unique. Instead you want an API that can support that data specifically, and hopefully quickly.
So long story short, you'll need an API for that.
Along the lines of what Shawn K says, perhaps create a View on your backend that represents the state of what is currently 'distinct', acknowledging that it might be stale/non-realtime. Then you could use the contents of that View to represent the choices available to the user. If generating the distinct set of values is non-performant, then if you're in a DB like Postgres (et al), create a Materialized View, refreshed on a timer.
The binding of the view data to the becomes the trick at that point, but there are probably clues to doing that here of SO and you could piece these two together.
BTW, I use Views regularly to handle edge certain edge cases like this. Beats caching data in a middle tier for sure.
How can i implement a KeywordFilter field to filter data from the database table as soon as text is fed into the field.
Most of the samples I have come across demonstrates filtering from predefined arrays.What i am looking out for is filtering from database.
Please guide how to go about it.Thanks
I have tried out this example of BB docs which shows in arrays
A straightforward approach would be to load the data from the database into a ReadableList and pass that to the KeywordFilterField.
The method used to set the values is
setSourceList(ReadableList list, KeywordProvider helper)
ReadableList is an interface which has a few implementations. The example code you are looking at uses the SortedReadableList but a BasicFilteredList would work nicely too.
I have that table:
create table t_place(
f_plc_timefrom time,
f_plc_timeto time,
f_plc_minute_cost Decimal(18,4)[24]
);
So, I can create array field, but I don't know, how can I fill in this array field in SQL code. I tryed to find way in many sources, but I could find nothing. I need your help.
AFAIK one can work with arrays only via API, there is no SQL syntax for that.
There is virtually no array support in Firebird in the query and procedural language. As ain says there is only some support via the API. Removal of the array functionality is on the table as well. See also ticket CORE-710.
In Rails 3.1, how would you go about making a query string appear cleaner and more readable.
For example, the Rails default for a key with multiple parameters in the query string would appear like:
/clients?ids[]=1&ids[]=2&ids[]=3
I want it to appear like:
/clients?ids=1,2,3
or even
/clients?ids=1|2|3
The ids are controlled by a series of links that act as filters for selecting 1 or more options for filtering on some search results.
What would be the best way to go about this?
The only way you could do it automatically is by monkeypatching something in Rack or Rails. You should avoid doing that as it's going to cause more issues than it solves.
If you have a string that embeds a | or , then you could have it incorrectly converting data. Or if you only have one entry such as ids=1 it wouldn't know to convert it into an array with just 1 inside.
You would be better off doing this manually whenever you need to pass an array that needs to be cleaned up. In this case, you would just call ids.join(",") when passing it to the router method and params[:ids].split(",") to get an array back out.