I am using Rails 4.2.3 and ruby ββ2.2.1
I wrote the scope in the role model as follows:
application / models / role.rb
scope :default, -> { find_by(default: true) }
Now when i run
> Role.default
As you can see, this causes 2 queries and returns the wrong result.
I tried using a class method instead of scope
def self.default self.find_by(default: true) end
Now when i run
Role.default #this is the output I got Role Load (0.2ms) SELECT `roles`.* FROM `roles` WHERE `roles`.`default` = 1 LIMIT 1 => nil
Using the class method, find_by works correctly.
I canβt understand what I am doing wrong here. Any help would be greatly appreciated. Thanks in advance.
source share