Model Using Modules in Rails Application

Loading module into User Model in Rails

You seem to have missunderstood what what modules and classes do in Ruby. In Ruby a module is simply an object that wraps a set of methods and constants.

A module can extend other modules, classes and objects and can be included in classes thus implementing multiple inheritance. Modules in Ruby fill the role that traits, namespaces and singletons do in other languages.

Classes are actually modules (Module is part of the ancestors chain of Class) with the key difference that you can make instances of a class and that class can inherit from a single other class and cannot extend other objects or be included.

The code example here actually doesn't make sense. If you want to declare a method that will be available to classes that include a module you want to declare it in the module itself:

module MyModule
def some_method
# do something
end
end

When you then call User#another_method it will look in the ancestors chain of the User class until it finds the method which is defined in MyModule.

module MyModule
class MyClass
def some_method
# do something
end
end
end

Will actually definte the class MyClass with an instance method that is only available to instances of MyClass. The only thing that the module does here is change the module nesting so that the class is defined in MyModule instead of the global namespace.

How to create and use a module using Ruby on Rails 3?

To 1. A module is created/opened
by simply saying:

module MyModule
def first_module_method
end
end

To 2. The lib folder. If you want to organize your modules in the lib folder, you can put them into modules themselves. For example, if you wanted a subfolder super_modules your modules would be defined as follows:

module SuperModules
module MyModule
def first_module_method
end
end
end

To 3./5. When including the module in a class you can simply call the modules methods as if they were defined within the class:

class MyClass
include MyModule
def some_method
first_module_method #calls module method
end
end

To 4.
Frst, make sure that your module is really needed in every class of your application. If it isn't it makes sense to only include it where it is need so as not to bloat the classes that don't need it anyways. If you really want the module everywhere, include look at the class hierarchy of your classes in the app. Do you want the module in all models? You could open ActiveRecord::Base and add add your module there.

How do I include a module method in a model with ruby on rails 4?

In previous versions of Rails, we needed to save files in the /lib folder, as we have the new folder app/models/concerns in Rails 4, we can directly call the method from the module in a model without any helper.
e.g:

app/models/concerns/MyModule.rb

    Module MyModule

extends .. #

def some_method_in_my_module
end

end

in our model we call it as:

    class ...

#

@usuario = MyModule.some_method_in_my_module

#

end

Where should I place my own module within rails application?

Rails < 5

Before Rails 5, the place to put reusable code such as this is in the lib directory. However you do not need to require anything as lib is already in the load path and it's contents will be loaded during initialization.

If you need to extend an existing class, you define your module first and then include it by sending it as a message to the class you wish to extend, e.g.

module MyExtensions
def self.included base
base.instance_eval do
def my_new_method

end
end
end
end

ActiveRecord::Base.send :include, MyExtensions

How to put many Model classes together in one Module to use in Rails

app/models/widgets/blue_widget.rb
class Widgets::BlueWidget < ActiveRecord::Base
// etc.
end

app/controllers/blue_widget_controller.rb
def index
@widgets = Widgets::BlueWidget.all
end

You can also namespace the controllers.

Edit:

lib/widgets.rb
module Widgets
class BlueWidget
end

class RedWidget
end
end

controller:
require 'lib/widgets'
def index
@widgets = Widgets::BlueWidget.all
end

Is that what you mean?

Cannot include module in model

include will treat those methods as instance methods, not class methods. What you want to do is this:

module LifeControl    
module ClassMethods
def alive
where('deleter is null')
end

def dead
where('deleter is not null')
end
end

def self.included(receiver)
receiver.extend ClassMethods
end
end

This way, alive and dead will be available on the class itself, not instances thereof.

Where to place own module in rails app?

These generally are placed in a new directory off app called concerns - it's worth googling for more info about how that works.

here's a good ref of places to go look: How to use concerns in Rails 4



Related Topics



Leave a reply



Submit