Rails - 如何在初始化器中设置全局实例变量?

时间:2022-08-14 23:34:15

I was using the predictor gem. I initialized the recommender in initializers/predictor.rb:

我正在使用预测宝石。我在initializers / predictor.rb中初始化了推荐器:

require 'course_recommender'    

recommender = CourseRecommender.new

# Add records to the recommender.
recommender.add_to_matrix!(:topics, "topic-1", "course-1")
recommender.add_to_matrix!(:topics, "topic-2", "course-1")
recommender.add_to_matrix!(:topics, "topic-1", "course-2")

And then I wanted to use the recommender in the CourseController like this:

然后我想在CourseController中使用推荐器,如下所示:

class CourseController < ApplicationController
  def show
    # I would like to access the recommender here.
    similiar_courses = recommender.similarities_for("course-1")
  end
end

How could I set recommender as an application controller variable so I could access it in the controllers?

如何将推荐器设置为应用程序控制器变量,以便我可以在控制器中访问它?

3 个解决方案

#1


36  

In your initilizers/predictor.rb you shall define your recommender not as:

在您的initilizers / predictor.rb中,您应该将推荐人定义为:

recommender = CourseRecommender.new

but as:

但是:

::Recommender = CourseRecommender.new

:: Recommender = CourseRecommender.new

this way you define a constant throughout the scope of your application, instead of defining a local variable. In your initializer and controller you access it as Recommender.

这样,您可以在应用程序的整个范围内定义常量,而不是定义局部变量。在初始化程序和控制器中,您可以将其作为推荐程序进行访问。

#2


3  

I solve the problem. But instead of setting a global instance, I use the Singleton pattern.

我解决了这个问题。但是我没有设置全局实例,而是使用Singleton模式。

Here's the code:

这是代码:

# lib/course_recommender.rb
require 'singleton'
class CourseRecommender
  include Predictor::Base
  include Singleton
  # ...
end

# initializers/predictor.rb
@recommender = CourseRecommender.instance

# Add records to the recommender.
@recommender.add_to_matrix!(:topics, "topic-1", "course-1")
@recommender.add_to_matrix!(:topics, "topic-2", "course-1")
@recommender.add_to_matrix!(:topics, "topic-1", "course-2")

# controllers/course_controller.rb
require 'course_recommender'
class CourseController < ApplicationController
  def show
    similiar_courses = CourseRecommender.instance.similarities_for("course-1")
  end
end

#3


2  

I am not familiar with that gem, but it seems like you should have your code in ApplicationController.

我不熟悉那个gem,但似乎你应该在ApplicationController中拥有你的代码。

in ApplicationController:

在ApplicationController中:

@recommender = CourseRecommender.new

# Add records to the recommender.
@recommender.add_to_matrix!(:topics, "topic-1", "course-1")
@recommender.add_to_matrix!(:topics, "topic-2", "course-1")
@recommender.add_to_matrix!(:topics, "topic-1", "course-2")

and then in your controler:

然后在你的控制器中:

class CourseController < ApplicationController
  def show
    # I would like to access the recommender here.
    similiar_courses = @recommender.similarities_for("course-1")
  end
end

#1


36  

In your initilizers/predictor.rb you shall define your recommender not as:

在您的initilizers / predictor.rb中,您应该将推荐人定义为:

recommender = CourseRecommender.new

but as:

但是:

::Recommender = CourseRecommender.new

:: Recommender = CourseRecommender.new

this way you define a constant throughout the scope of your application, instead of defining a local variable. In your initializer and controller you access it as Recommender.

这样,您可以在应用程序的整个范围内定义常量,而不是定义局部变量。在初始化程序和控制器中,您可以将其作为推荐程序进行访问。

#2


3  

I solve the problem. But instead of setting a global instance, I use the Singleton pattern.

我解决了这个问题。但是我没有设置全局实例,而是使用Singleton模式。

Here's the code:

这是代码:

# lib/course_recommender.rb
require 'singleton'
class CourseRecommender
  include Predictor::Base
  include Singleton
  # ...
end

# initializers/predictor.rb
@recommender = CourseRecommender.instance

# Add records to the recommender.
@recommender.add_to_matrix!(:topics, "topic-1", "course-1")
@recommender.add_to_matrix!(:topics, "topic-2", "course-1")
@recommender.add_to_matrix!(:topics, "topic-1", "course-2")

# controllers/course_controller.rb
require 'course_recommender'
class CourseController < ApplicationController
  def show
    similiar_courses = CourseRecommender.instance.similarities_for("course-1")
  end
end

#3


2  

I am not familiar with that gem, but it seems like you should have your code in ApplicationController.

我不熟悉那个gem,但似乎你应该在ApplicationController中拥有你的代码。

in ApplicationController:

在ApplicationController中:

@recommender = CourseRecommender.new

# Add records to the recommender.
@recommender.add_to_matrix!(:topics, "topic-1", "course-1")
@recommender.add_to_matrix!(:topics, "topic-2", "course-1")
@recommender.add_to_matrix!(:topics, "topic-1", "course-2")

and then in your controler:

然后在你的控制器中:

class CourseController < ApplicationController
  def show
    # I would like to access the recommender here.
    similiar_courses = @recommender.similarities_for("course-1")
  end
end