-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default []
on type: :array
is returning the exact same 'default' object
#113
Comments
This is a Ruby thing. Once you assign an array, I believe it will be a different object. Otherwise, you'll have to copy the array (dup). You can test this:
Steve |
Yep, although my use case is that I just want to append items directly to the Cheers, Nathan Kot On Mon, Apr 21, 2014 at 09:26:38AM -0700, s.ross wrote:
|
I wonder if a more general solution might be to have the default take a value or a proc. So something like this would work:
This would let you do things like generate UUIDs and stuff as defaults (!). Or is the syntax too ugly for words? WDYT? |
Hmmmmmmm, I think its a bit ugly xD I wonder how ActiveRecord does it? When I used it never ran into this problem before, so I'm assuming they do it Nathan Kot On Mon, Apr 21, 2014 at 03:44:18PM -0700, s.ross wrote:
|
Well, you wouldn't need the dup:
Perhaps it would make sense to clone a non-primitive default value when creating an instance, but then you have to decide, shallow, deep, etc. Honestly I think rails can be a little to clever sometimes, I think the above is clear, it's saying that the default should be evaluated at the time it is created. Rails moved in this direction anyway, i.e. using procs for scopes and things like that. |
I'm inclined to agree with @aceofspades on this. ActiveRecord doesn't really address mutable defaults (for lack of a better term). They defer it to after hooks. A stubby proc, lambda or method would be a clean general solution, IMO. |
I can live with the shorthand :) wonder if it's already supported by motion model? |
Addresses issue #113. Allows specification of a proc, block, or sym for default values.
I pushed a fix for this in the proc_defaults branch (https://github.com/sxross/MotionModel/tree/proc_defaults). Please have a look. |
perfect! looks good 👍 |
For example:
Not sure if desirable? Happy to look into this if not.
The text was updated successfully, but these errors were encountered: