Jump to content

The ultimate community for Ruby on Rails developers.


Photo

scope in model is bypassing conditions


  • Please log in to reply
No replies to this topic

#1 swigrid

swigrid

    Passenger

  • Members
  • 9 posts

Posted 05 March 2014 - 12:23 PM

hello all,

 

i have scope in my model as following

class Treatment::Code < ActiveRecord::Base
  belongs_to :treatment_rule, :class_name => 'Treatment::Rule'

  scope :groups, group('"group"').pluck(:group)
end

here are codes for my rules:

>> rule.treatment_codes
#<ActiveRecord::Associations::CollectionProxy [#<Treatment::Code id: 7, treatment_rule_id: 2, code: "500", group: 1, created_at: "2014-03-05 11:46:23", updated_at: "2014-03-05 11:46:23">, #<Treatment::Code id: 8, treatment_rule_id: 2, code: "520", group: 1, created_at: "2014-03-05 11:46:23", updated_at: "2014-03-05 11:46:23">]>

when i group them by group without scoping working ok

>> rule.treatment_codes.group('"group"').pluck(:group)
[1]

when i group them by my scope it gives me groups for all roles and not only for rules which i m working currently with

>> rule.treatment_codes.groups
[1, 2]

i mean its not big deal, i can use grouping as in first example but i would be wondered why's that and if this can be tweaked that scope would be scoping only for given rule and not for all rules

 

thanks

Roman






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users