Add data type support to store_accessor - #18942
Conversation
There was a problem hiding this comment.
keys + typed_keys.keys
|
@palkan Looks good. I just added some minor comments about formatting :) |
|
Isn't there chance to use https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attributes.rb for similar purpose? |
be754ee to
9fd3a39
Compare
|
@simi What do you mean? To override store type with custom type? |
There was a problem hiding this comment.
Perhaps next phase, but would be cool to allow users api to manage these mappings, similar to how you can use the attribute :some_attribute, :some_user_defined_type.
There was a problem hiding this comment.
Also, perhaps deprecate store_accessor method name in favor of store_attribute? It'd be more in-line with new attribute API, and also make more semantic sense because accessor implies a basic in-memory ruby getter/setter, while attribute macro does casting and interacts with db.
Alternatively this could be attribute :something, store: true or attribute :something, store: { some_attr: :some_type }.
CC @sgrif
There was a problem hiding this comment.
Yeah. It's late, and I'll look at this more in the morning, but I'd be against this feature overall and definitely this should lean in that direction implementation wise.
There was a problem hiding this comment.
@egilburg
Store accessor (or store attribute) is very different from attribute, because it doesn't deal with raw db data (which varies for different store types). Kind of another layer of abstraction.
I think that solution should be as simple as possible. Store accessor is just a getter/setter, right. The idea is to make these getters/setters a little bit smarter.
There was a problem hiding this comment.
Kind of another layer of abstraction.
Yes. Agree, but this doesn't mean we can't use the same concept of type casting applied on attribute for this feature too.
There was a problem hiding this comment.
@rafaelfranca
Oh, there is a new API method ActiveRecord::Type.lookup, it's just what we need here. Great. I gonna patch the code!
5af3b71 to
2892472
Compare
|
Updated using class Account < ActiveRecord::Base
# this method allows type options
store_attribute :data, :progress, :float, precision: 2
endBut I think it's still useful to have shorthand syntax within |
|
I'll take a closer look at this tomorrow. |
|
|
2892472 to
11df2e1
Compare
|
Updated docs |
There was a problem hiding this comment.
Why did we need to move this out of _prepare_local_stored_attributes?
There was a problem hiding this comment.
You mean smth like _prepare_local_stored_attributes(store_name, keys)? Reasonable.
There was a problem hiding this comment.
Oh I see, the diff didn't make it clear that is a new method.
|
So there's a couple of problems here. First is that the My personal opinion is that if you get to this point, you should just have separate columns for the values rather than putting it in a store accessor. However, if we do go this route, I think we should take it a step further and promote the accessors to full-blown attributes. (At this point I think anything that defines This gets into a feature that I'm actively working on, which is attribute composers. I'd prefer if we held off on this feature until I've got that API a little bit more fleshed out, but this should be able to be built on top of that a bit more easily. I'm still wrapping up the standard attributes API, so it's just a bit too soon for me to hash out too many specifics on composers, and at this point introducing new code with its own semantics could potentially make implementing that more difficult. I hope my concerns make sense. Let me know what you think. |
There was a problem hiding this comment.
Please use doulbe quoted strings consistently.
|
Totally agree. |
|
Yes, but my previous comment is invalid given #18942 (comment) |
11df2e1 to
a92447f
Compare
|
I came up with the following:
So, type casting occurs:
/cc @sgrif |
87542ff to
ea7094b
Compare
df60c9b to
1744e39
Compare
|
ping @sgrif @rafaelfranca |
1744e39 to
4917117
Compare
4917117 to
394ea9d
Compare
@sgrif is this something you are still considering? I know many things changed since February, and I was wondering what you think now about this ticket. Thanks and happy holidays! 🎄 |
Add type casting for store_accessors. First, example:
This feature is the most efficient when working with Postresql hstore, because hstore stores all values as strings. Example:
This makes code depend less on store type.
Another feature is data normalization:
Custom types are also supported (they must inherits from
ActiveRecord::Type::Value):