i was wondering what was the best way to uniquely identify rails objects. im storing them in an index, and currently i have been doing so like
$index.document(#paper.id).add(fields, :variable => variables)
where the #paper.id uniquely identifies it. however when i store other objects, ie #card
$index.document(#card.id).add(fields, :variable => variables)
it's going to overwrite my index because im using the databases autoincrement (which is not unique because they all start and increment by 1). what would be the best solution to having unique identifiers across all my rails objects?
i looked at UUID's and had some reservations about making them my primary key like in...
http://ariejan.net/2008/08/12/ruby-on-rails-uuid-as-your-activerecord-primary-key/
i could always make a separate column i guess. or i could do something like get the object's id and then append it to creation_time. are these good ideas? if not, what would be a good solution for this?
thanks!
I would go with a composite of the object id and the name of the objects table.
Related
I am quite a newbie to Cube.js. I have been trying to integrate Cube.js analytics functionality with my Ruby on Rails app. The database is PostgreSQL. In a database, there is a certain column called answers_json with jsonb data type which contains a nested hash. An example of data of that column is:
**answers_json:**
"question_weights_calc"=>
{"314"=>{"329"=>1.5, "331"=>4.5, "332"=>1.5, "333"=>3.0},
"315"=>{"334"=>1.5, "335"=>4.5, "336"=>1.5, "337"=>3.0},
"316"=>{"338"=>1.5, "339"=>3.0}}
There are many more keys in the same column with the same hash structure as shown above. I posted the specific part because I would be dealing with this part only. I need assistance with accessing the values in the hash. The column has a nested hash. In the example above, the keys "314", "315" and "316" are Category IDs. The keys associated with Category ID "314" are "329","331","332", "333"; which are Question IDs. Each category will have multiple questions. For different records, the category and question IDs will be dynamic. For example, for another record, Category ID and Question IDs associated with that category id will be different. I need to access the values associated with the key question id. For example, to access the value "1.5" I need to do this in my schema file:
**sql: `(answers_json -> 'question_weights_calc' -> '314' ->> '329')`**
But the issue here is, those ids will be dynamic for different records in the database. Instead of "314" and "329", they can be some other numbers. Adding different record's json here for clarification:
**answers_json:**
"question_weights_calc"=>{"129"=>{"273"=>6.0, "275"=>15.0, "277"=>8.0}, "252"=>{"279"=>3.0, "281"=>8.0, "283"=>3.0}}}
How can I know and access those dynamic IDs and their values since I also need to perform mathematical operations on values. Thanks!
As a general rule, it's difficult to run SQL-based reporting on highly dynamic JSON data. Postgres does have some useful functions for dealing with JSON, and you might be able to use json_each or json_object_keys plus a few joins to get there, but its quite likely that the performance and maintainability of such a query would be difficult to say the least ๐
Cube.js ultimately executes SQL queries, so if you do go the above route, the query should be easily transferrable to a Cube.js schema.
Another approach would be to create a separate data processing pipeline that collects all the JSON data and flattens it into a single table. The pipeline should then store this data back in your database of choice, from where you could then use Cube.js to query it.
I wonder if it's possible to to run Model.create() such that instead of taking next free id integer it takes the lowest free integer.
For example, assume we have records for id=10..20 and we don't have records for id=0..9, I want create instance of Model with id starting from 0 (in normal Mode.create() in would create instance staring from 21)
Preferably I want to do it in automatic manner. I don't want to change id by explicitly defining it.
DB
You'll be best doing this at database-level (look at altering the auto-increment number)
Although I think you can do this in Rails, I would highly recommend using the DB functionality to make it happen. You can do something like this in PHPMYAdmin (for MYSQL):
If you set the Auto-Increment to the number you wish to start at, every time you save data into the DB, it will just save with that number. I think using any Rails-based method will just overcomplicate things unnecessarily.
I'd discourage it.
Those ids serve solely as unique identifiers for rows in a table, and it's the database's job to assign one. You can verify that the model doesn't require an id to be saved:
m = Model.new
# populate m with data
m.name = "Name"
# look at what m contains
m
# and save it
m.save
# now inspect it again and see it got its unique id
m
While it might be possible to modify ids, it's not a good practice to give more sense to ids โ when each new record gets a unique id at any time it's easier to debug possible DB structure errors that might occur during development. Like, say, some associated objects suddenly show up in a new user's account. Weird enough, right? That can happen and, worst case, can show up in production resulting in a severe security breach.
Keeping ids unique at all times eliminates this bug's effect. That seems much more important if the associated objects store confidential information and you care about keeping them safe. Encryption concerns aside.
So, to be sure in every situation, developers have adopted a practice of not giving id any other role other than uniquely identifying a row in a table. If you want it to do something else, consider making another field for that purpose.
I'm trying to store two types of product identifications together, one for each product color for each product.
I want to do this without creating another table, just by storing a set of id pairs. My form looks like this:
How can i submit and store this data in a consistent way? Is Hstore a good alternative for this?
Why donโt you want to create another table? Your data model seems to necessitate it. I would recommend avoiding database-specific solutions when a Rails-based one (adding another model) would be more universal and is trivially supported.
I have a weird situation. I have two models ("object" and "asset") and a polymorphic join table (things other than objects can relate to assets). The "object" has a serialized hash, where some of the values are ID numbers (foreign keys, although not marked as such in the db) for "assets", and I would like to properly track which "assets' are being used in which "objects" in that join table, and I'm not sure of the best way to go about it.
The main purpose of this set up is to allow us to list all the objects, etc, that use a given asset. Many of the other things that use assets also have such a serialized hash situation.
What I think I need to do is just update the entries in the join table based on a simple scan of the hash for the ID values, and, from there, just treat it like any many-to-one situation.
But, I also wonder if there's a better way to go about it?
(I can't really change the architecture - the serialized hash is actually for use in a completely different codebase, and while I technically don't have to use the join table, I'd prefer to not add another way to do the book-keeping on this kind of relationship.)
PS - Rails on Postgres.
something like unique column in sql. Any suggestion?
Your question is quite "open", so I tried to figure out what you want to do.
If you need to add a column which is not the primary key to store something like a unique ID, you can store there an erlang reference (Ref = make_ref()). which is almost guaranteed to be unique (cycle around 2^82). I don't know what is the behavior in multinode, but if there is a problem it is possible to tag the record with {node(),make_ref()}.
if you want create unique records by the combination of several keys: K1,K2,K3 you can use the tuple {K1,K2,K3} as key of the table and use a set or ordered set. but it will more complex to look into the table
if it it something else, some complementary information could help.