Getting around this data store thing that rails provide, I have something that troubles me
Let's say I have a simple post model, with a title
, and that for some reason I'd like to store somehow dynamics attributes in another field, call "post_garbage
"
In the post model, I have the simple serializer :
class Post < ActiveRecord::Base
serialize :post_garbage
end
As I said, and besides what the rails doc explains, no named accessors are set on this post_garbage
field
Playing in the console :
p = Post.first
p.post_garbage["the_stuff"] = "something"
p.save
I get an error, obviously. "the_stuff" is unknown. Stopping right here, you'd like to dump this data_store thing, safer and easier to create a new field to get the job done (I don't get the utility of it if you have to set an accessor on a data_store field).
* But *
If first an empty hash is saved in this post_garbage
field :
p = Post.first
p.post_garbage = {}
p.save
The following becomes possible :
p = Post.first
p.post_garbage["whatever_i_have_in_mind"] = "some value"
p.save
Right after that :
p.post_garbage["it_then_get_dynamic"] = "and that's pretty cool"
p.save
It gets schemaless with no need to specify accessors; kind of provide the expected thing. So I wonder : many would expect a schemaless thing with the datastore, but it is not said to be used like this by the doc. But it works, with what looks like a bad trick. Wonders…
In the opposite, what's the use of the serialize :post_garbage
, if, when empty, it does not returns an empty hash ?
I'd really appreciate some experienced feedback on this one as I feel like I miss something
Thanks a lot