diff options
author | Heiko Bernloehr <Heiko.Bernloehr@FreeIT.de> | 2017-03-11 03:14:58 +0100 |
---|---|---|
committer | Heiko Bernloehr <Heiko.Bernloehr@FreeIT.de> | 2017-03-11 03:14:58 +0100 |
commit | ca39c78a27392505e0d34f74d973734c6b6b2460 (patch) | |
tree | d8ab265890c5fa1d2f974deaff31505a73fadba8 | |
parent | c127bbee594514519db4f142a0db6ac6b3dbcdd9 (diff) | |
download | ecs-ca39c78a27392505e0d34f74d973734c6b6b2460.tar.gz ecs-ca39c78a27392505e0d34f74d973734c6b6b2460.zip |
Lazy evaluation of ActiveRecord::Relation.
-rw-r--r-- | app/models/message.rb | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/app/models/message.rb b/app/models/message.rb index 742a9c1..ee267c6 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -81,13 +81,22 @@ class Message < ActiveRecord::Base transaction do create_update_helper(request, app_namespace, ressource_name, participant.id) save! - receivers_old = Participant.for_message(self).uniq + # NOTE Unit test doesn't pass if we are not explicitely accessing + # the ActiveRecord::Relation receivers_old and receivers_new, beacause + # of Rails's "lazy evaluation". + # ("MessageTest#test_update_receivers [/home/mb7/freelancer/projekte/ecs4/test/models/message_test.rb:56]") + # With accessing the ActiveRecord::Relation with #to_a we force an evaluation + # by converting ActiveRecord::Relation to an array of Model-Objects. We could + # have done this # also by calling #load!, but would have get back still an + # ActiveRecord::Relation, which works also. + receivers_old = Participant.for_message(self).uniq.to_a + #puts "*** receivers_old: #{receivers_old.inspect}" MembershipMessage.de_populate_jointable(self) MembershipMessage.populate_jointable(self, request.headers["X-EcsReceiverMemberships"], request.headers["X-EcsReceiverCommunities"], participant) - receivers_new = Participant.for_message(self).uniq + receivers_new = Participant.for_message(self).uniq.to_a # TODO: if there are only the headers X-EcsReceiverMemberships and # X-EcsReceiverCommunities are updated, then we have to generate events only # for these new and removed receivers. To distinguish if the message body @@ -127,7 +136,8 @@ class Message < ActiveRecord::Base # return first messages from fifo/lifo queue def self.fifo_lifo_rest(namespace, ressource, participant_id, options={:queue_type => :fifo}) - m = all.readonly(false).select(:id). + m = all. + readonly(false). joins(:ressource, :membership_messages => { :membership => :participant }). where(:participants => { :id => participant_id }, :ressources => { :namespace => namespace, :ressource => ressource }). |