Permalink
Browse files

reset `@arel` when modifying a Relation in place.

  • Loading branch information...
1 parent 10ed701 commit 1b7aa62b184c4410c99208f71b59bbac5c5f03be @senny senny committed Apr 24, 2014
@@ -1,3 +1,8 @@
+* Reset the cache when modifying a Relation with cached Arel.
+ Additionally display a warning message to make the user aware.
+
+ *Yves Senn*
+
* PostgreSQL should internally use `:datetime` consistently for TimeStamp. Assures
different spellings of timestamps are treated the same.
@@ -84,7 +84,7 @@ def through_scope
end
scope.references! reflection_scope.values[:references]
- scope.order! reflection_scope.values[:order] if scope.eager_loading?
+ scope = scope.order reflection_scope.values[:order] if scope.eager_loading?
@tenderlove

tenderlove Apr 24, 2014

Owner

Why is 'references!' Ok, but 'order!' isn't? What in this code is making arel be loaded?

@senny

senny Apr 24, 2014

Member

.eager_loading? is loading the arel.

end
scope
@@ -64,6 +64,7 @@ def #{name}_values # def select_values
#
def #{name}_values=(values) # def select_values=(values)
raise ImmutableRelation if @loaded # raise ImmutableRelation if @loaded
+ check_cached_relation
@values[:#{name}] = values # @values[:select] = values
end # end
CODE
@@ -81,11 +82,22 @@ def #{name}_value # def readonly_value
class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{name}_value=(value) # def readonly_value=(value)
raise ImmutableRelation if @loaded # raise ImmutableRelation if @loaded
+ check_cached_relation
@values[:#{name}] = value # @values[:readonly] = value
end # end
CODE
end
+ def check_cached_relation # :nodoc:
+ if defined?(@arel) && @arel
+ @arel = nil
+ ActiveSupport::Deprecation.warn <<-WARNING
+Modifying already cached Relation. The cache will be reset.
+Use a cloned Relation to prevent this warning.
+WARNING
+ end
+ end
+
def create_with_value # :nodoc:
@values[:create_with] || {}
end

6 comments on commit 1b7aa62

Member

senny replied Apr 24, 2014

The root of this change was #14848

arel replied Apr 27, 2014

I'm being loaded?

Member

vipulnsward replied Apr 27, 2014

@arel you were not.

Owner

rafaelfranca replied Jan 4, 2015

@senny how would we remove this deprecation?

Owner

matthewd replied Jan 5, 2015

@rafaelfranca I guess it should start raising ImmutableRelation, as it does for @loaded

Member

senny replied Jan 5, 2015

@rafaelfranca deprecation removed in 3ae9818

Please sign in to comment.