Cannot access with_scope from a mixin - ruby-on-rails

I just stumbled over a weird problem, and I don't really understand what is causing this.
In our rails app, let's have a mixin Mixin:
module Mixin
def foo
with_scope :find => ... do
...
end
end
end
which is includeed into a model class elsewhere:
class Model < ActiveRecord::Base
include Mixin
...
end
calling Model.new.foo results in an error: NoMethodError: undefined method with_scope
I then changed the foo method to:
def foo
self.class.with_scope :find => ... do
...
end
end
But this also results in an error: NoMethodError: protected method with_scope called
This seems odd. I would have expected that the mixin methods would behave like any other method in Model. I never stumbled over this before, because all the instance methods like save are there and work as usual.
Am I doing it all wrong?

Related

Ruby On Rails : Calling class method from concern

In Rails, is it possible to call methods from the class that included the concern, in the concern itself ? ie:
class Foo < ApplicationRecord
include Encryptable
def self.encrypted_attributes
%i[attr_1 attr_2]
end
end
module Encryptable
extend ActiveSupport::Concern
included do
self.encrypted_attributes do |attr|
define_method("#{attr}=") do |arg|
# do some stuff
end
define_method("#{attr}") do
# do some stuff
end
end
end
end
The issue is, when I try to do that, I get an error like :
*** NoMethodError Exception: undefined method 'encrypted_attributes' for #<Class:0x00005648d71c2430>
And, when debugging inside the concern, I get this something like this :
(byebug) self
Foo (call 'Foo' to establish a connection)
(byebug) self.class
Class
Ruby is a scripting language and the order matters. The following would do:
class Foo < ApplicationRecord
def self.encrypted_attributes
%i[attr_1 attr_2]
end
# OK, now we have self.encrypted_attributes defined
include Encryptable
end
More info: ActiveSupport::Concern#included.

Why isn't a module being read by my View-backed Model?

My controller calls the method bar:
class CompsController < ApplicationController
include ApplicationHelper
def quick_create
#var = Matview.bar #projects
end
end
bar is defined in a model that represents a materialized view in my database (it is not in my schema):
class MatView < ActiveRecord::Base
include ApplicationHelper
table_name = 'mat_views'
def self.bar(arg)
foo arg
end
end
'bar' calls method foo, which is defined in my ApplicationHelper:
module ApplicationHelper
def foo(arg1)
#do stuff
end
end
I've included the ApplicationHelper in both my controller and model, and yet I get this error:
NoMethodError in CompsController#quick_create
undefined method `foo' for Matview(Table doesn't exist):Class
why?
Matview.bar #projects
Is calling a class level method on the MatView class.
But your foo and bar are both instance method definitions. To make them class methods, you need def self.bar(arg) or def self.foo(arg1)
And to get class methods into your ActiveRecord model, you need to extend, not include the module:
class MatView < ActiveRecord::Base
extend ApplicationHelper
end
Or, if that does not sound like what you meant to do, then maybe you meant to do:
Matview.new.bar #projects
in which case the instance methods like you wrote them should work.

Undefined method despite including my module

I have this module:
module Moody
def foo
'bar'
end
end
And yet in a controller:
class MyController
include Moody # this works. Including Moodasdasd causes a failure
def index
puts foo #=> expect 'bar' undefined local variable or method `foo' raised
end
end
I get a undefined local variable or method error.
How should I include a module's methods in an action?
I'm guessing the module isn't being included in the application namespace?
I can't be sure without knowing where the module file is located.
Try and include the filepath in the application namespace and see if that helps.
Like in this gist: https://gist.github.com/jhjguxin/5180534

How does one make a Model perform a method on itself?

class MyAwesomeClass
def foobar
puts "trip!"
end
So that I can perform :
MyAwesomeClass.foobar
=> "trip!"
I keep getting :
NoMethodError: undefined method `foobar' for MyAwesomeClass:Class
class MyAwesomeClass
def self.foobar
puts "trip!"
end
end
Using "self" makes the method a class instance method

Rails: callbacks from module

I try to do this:
app/models/my_model.rb:
class MyModel < ActiveRecord::Base
include MyModule
...
end
lib/my_module.rb:
module MyModule
before_destroy :my_func #!
def my_func
...
end
end
but I get an error:
undefined method `before_destroy' for MyModule:Module
How can I correct it.
Also I'm new to ruby. What type has these "attributes": before_destroy, validates, has_many?
Are they variables or methods or what?
Thanks
before_destroy, validates, etc. are not attributes or anything like that. These are method calls.
In ruby, the body of a class is all executable code, meaning that each line of the class body is executed by the interpeter just like a method body would.
before_destroy :my_func is a usual ruby method call. The method that gets called is before_destroy, and it receives a symbol :my_func as an argument. This method is looked up in the class (or module) in the scope of which it is called.
So moving on to your question, I think now you should understand that when the interpreter loads your module
module MyModule
before_destroy :my_func #!
def my_func
...
end
end
it starts executing its body and searches for the method before_destroy in this module and cannot find one. What you want to do is call this method not on the module, but rather on the class where the module is included. For that we have a common idiom using the Module#included method:
module MyModule
module InstanceMethods
def my_func
...
end
end
def self.included(base)
base.send :include, InstanceMethods
base.before_destroy :my_func
end
end
In lib/my_module.rb, do this:
class MyInheritedClass < ActiveRecord::Base
before_destroy :my_func
def my_func
...
end
end
In app/models/my_model.rb, do this:
class MyModel < MyInheritedClass
...
end
There is no before_destroy filter in the module you are trying to create above. What my code does is creating a class that will inherit from ActiveRecord::Base and that will be your template class which all your other classes can inherit from. The template class also contains all properties of ActiveRecord::Base.
u can correct this by removing before_destroy from MyModule and place it in MyModel instead
before_destroy and other callbacks are only available to classes which extends ActiveRecord::Base, more info here
hope this helps =)

Resources