how to add a class method to Mongoid::Criteria? - ruby

I want to add a new class method to Mongoid::Criteria, all models can use this method, for example:
class User
include Mongoid::Document
......
end
then I add below code to rails initialize
class Mongoid::Criteria
def use_master
read(mode: :primary_preferred)
end
end
but it doesn't work:
pry(main)> User.use_master
NoMethodError: undefined method `use_master' for User:Class
How to add the "use_master" method to model? Any help in advanced!

Read preference can be changed via persistence context:
User.with(read: {mode: :primary_preferred}) do
# ...
end
This is explained in more depth in Mongoid manual: https://docs.mongodb.com/mongoid/master/tutorials/mongoid-persistence/#runtime-persistence-options

I found a good solution:
module Mongoid
module Findable
def use_master
read(mode: :primary_preferred)
end
end
end

Related

Use a class instance inside a module

I'm trying to do this:
example_module.rb
module ExampleModule
def example_method
...
ExampleClass.new.do_something(arg_one, arg_two)
end
end
example_class.rb
class ExampleClass
def initialize()
...
end
def do_something(arg_first, arg_second)
...
end
end
#>> Runtime error - uninitialized constant ExampleModule::ExampleClass
Is it possible to use a class instance inside a module? I found examples that do the opposite; they use a module to extend a class. What am I missing? Is there an article that explains this better?
Add this on top of your example_module.rb file.
require ./example_class'

Why am I getting a NoMethodError when calling an instance method from global scope?

I have searched around for the answer to this and I can see a lot of similar problems but I still do not understand what I am doing wrong here. I have declared a Ruby class and attempted to new it and then call some instance methods on the instance, so why do I get the NoMethodError on my start method?
class MyClass
def initialize
self.class.reset
end
def self.reset
...
end
def self.start(port)
...
end
end
test = MyClass.new
test.start '8082' <- here <- undefined method `start' for #<MyClass:0x2f494b0> (NoMethodError)
As you can see I am a Ruby noob. Any help would be appreciated. I can change my class structure but I would really like to understand what I am doing wrong here.
here start is a class method.
By your current approach, you can use it in the following way
MyClass.start '8080'
But if you want to use it on instance of class then use the following code
class MyClass
def initialize
self.class.reset
end
def self.reset
...
end
def start(port)
...
end
end
test = MyClass.new
test.start '8080'
You are using start as a Class variable, the method names preceded with self-keyword make those methods as Class methods. So if you really want to not change your class then you should call it like this:
MyClass.start '8080'
Else you can remove the self from your reset and start methods and make them as Instance methods and use them as:
test = MyClass.new
test.start '8082'

NoMethodError in ruby module

module Add
def addition
sum=1+2
puts sum
end
a=Add.addition
Can anyone tell me what I'm missing and why I am getting this error->
undefined method `addition' for Add:Module (NoMethodError)
You are confusing class methods and instance methods. Your definition:
module Add
def addition
...
end
end
defines methods on instances of Add whereas you called a method on the module Add. If you want to define a class/module method, you need to define like:
module Add
def self.addition
...
end
end
If you want to be able to call it directly, define it as a directly accessible method:
def self.addition
# ...
end
Or you can always rework this using:
module Add
# ...(methods)...
extend self
end
Where that will automatically promote all mixin-type methods as being directly accessible.
You can also tag them more selectively like this:
module Add
def addition
# ...
end
module_method :addition
end
That method is then available either as Add.addition or if some other module or class calls include Add.

Rails: class methods helpers from Lib

Sorry the title may not be very clear. Basically I have a wrapper for the Split gem called ABFeature in lib/ab_feature/ab_feature.rb
In my view I want to be able to call my helpers like this:
ABFeature.current_settings
But this is not working, here is the error I have:
undefined local variable or method `session' for ABFeature:Module
session is a method from ActionController and is seems I can't access it...
Here is my code:
require 'split'
module ABFeature
class << self
include Split::Helper
def current_settings
...
end
end
end
class ActionController::Base
ActionController::Base.send :extend, ABFeature
end
Any idea?,
Greg
I'm not sure what the result should be but if you want the current_settings method available in the controller I think you can do
module ABFeature
include Split::Helper
def current_settings
end
end
and then
class ApplicationController < ActionController::Base
include ABFeature
end
I think you usually call helpers as instance methods. Then they should share the context with the controller.

How to determine table name within a Rails 3 model class

I want to get table name in a model method. I found there should be method table_name but when I try to call it I get NameError Exception: undefined local variable or method `table_name'. It is obviously not there:
pp methods.grep(/^ta.*/)
["table_name_prefix?",
"table_name_suffix?",
"taint",
"taguri",
"taguri=",
"tainted?",
"table_name_prefix",
"table_name_suffix",
"tap"]
How to get a "real" table name (no lowecase - pluralize tricks)?
Thanks
But I need that information in the
model's instance method. How to get
it?
You can simply do this in your instance method:
class Model
def instance_method
puts Model.table_name
end
end
Found it.
It's a class method. Its not so obvious from the Rails 3 documentation.
self.class.table_name
If you are in a class method of the class you want the table name for, try:
class Model < ActiveRecord::Base
def self.class_method
puts self.table_name
end
end
If you try using
self.class.table_name
you'll run into a NoMethodError: undefined method 'table_name' for Class:Class

Resources