A
A
AlexeyMoshkin2016-08-03 08:05:28
Ruby on Rails
AlexeyMoshkin, 2016-08-03 08:05:28

How to access models that are stored in the gem?

There is a rails application, most of which, including models, is in an included gem.
I'm writing a class to use it in rake task'e, it's in /lib. The class needs access to the application's models. How to get it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Katkov, 2016-08-03
@AlexeyMoshkin

let's say you write your_task.rake file
you can write it like this and load the default rails environment (note the :environment )

namespace :your_task do
  desc "my description"
  task :my_task => :environment do
    <...>
  end
end

but you can also drop :environment and manually upload the file with:
require_relative '../lib/your_path'

namespace :your_task do
  desc "my description"
  task :my_task do
    <...>
  end
end

A
Alexander Shvaikin, 2016-08-03
@shurik_sh

All gems are loaded into memory when a rail application is initialized, so all objects are available in the code without additional libraries. The plug-in library model is a regular ruby ​​class, most likely you can get to it by the full path, for example. SuperGem::SuperAR::SuperModel
You need to read the api documentation of the gem or look at the source code to understand how it works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question