5 ms·
The new Attributes API is interesting. https://github.com/rails/rails/blob/8c752c7ac739d5a86d4136ab1e9d0142c4041e58/activerecord/lib/active_record/attributes.rb
by pmontra 11y ago
The new Attributes API is interesting. https://github.com/rails/rails/blob/8c752c7ac739d5a86d4136ab1e9d0142c4041e58/activerecord/lib/active_record/attributes.rb https://github.com/rails/rails/blob/8c752c7ac739d5a86d4136ab...
Basically it's a type system for models with the ability of defining new types. I wonder if it can be used also for plain Ruby objects but given the existence of tableless ActiveRecord gems, why not?
https://github.com/softace/activerecord-tableless/ https://github.com/softace/activerecord-tableless/
- ScotterC 11y agoDidn't know about ActiveRecord-tableless thanks! Glad to have an easier way to incorporate AR benefits without including one by one
- tomphoolery 11y agoThat's pretty cool. Basically including the best parts of ActiveAttr into ActiveRecord. :) Maybe someday we won't have to write migrations anymore. Rails could maybe just diff the attributes and types given, and figure out the schema itself.
- thirdsun 11y agoOh yes, I always though it would make more sense to setup columns in the model itself, without migrations. Also, no more need for the annotate gem.
- pmontra 11y agoI see your point but the implementation must be very well designed. There are migrations that can take hours to complete in production. Examples: adding an index to a very large table or adding an element to an enum (I remember a customer of mine had this problem on a MySQL db a couple of years ago.) A DSL that makes clear you are modifying the db is a safeguard against those surprises. Just adding something like indexed: true to an attribute declaration would be dangerous. Furthermore rake migrate:up/down are more convenient than reverting a change to a model by checking out an older version. I believe that it's not impossible to do it, but it must be designed very carefully. Maybe because of those reasons Phoenix, the Rails like web framework for Elixir, has migrations even if models declare the attributes of the tables in the database.
- xentronium 11y agoThat's what original datamapper did. There are several problems with this approach, and basically you need to have a migration mechanism anyway.