7

I am new in Ruby on Rails and i am using Ruby version 2.1.0 and Rails 4.0.2

My Query is:-

I want to call Model in initializers.

my Model file is setting.rb and Model name Setting. its location is app/model directory.

I want to call Setting Model in initializers file paypal.rb.

paypal.rb location is config/initializers/paypal.rb.

Please help how to call Model in initializers in Ruby on Rails.

Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
  • Please share the code you have written, and tell us the error that you are getting when you try. – Taryn East Mar 27 '14 at 06:43
  • Please share your code like @TarynEast said - also, what happens if you try to add `require models/setting` at the top of your `paypal.rb` file? – nicohvi Mar 27 '14 at 06:47

4 Answers4

22

Do you want to make sure all other initializers have run before running this one? If so you could do this:

# config/initializers/paypal.rb
Rails.configuration.after_initialize do
  paypal_settings = Setting.find_by(name: "paypal")
  # do something with paypal settings...
end
rainkinz
  • 10,082
  • 5
  • 45
  • 73
  • I'm not OP, but I ran into the same issue, and I get a the following bug (in my case it's `User` instead of `Setting`): /usr/local/lib/ruby/gems/2.7.0/gems/activerecord-6.0.3.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `attr_accessible' for User (call 'User.connection' to establish a connection):Class (NoMethodError). I'm going to investigate whether it has to deal with a missing database connection, but otherwise do you have any other insight? – Robert Vunabandi Jun 14 '20 at 17:57
  • Never mind what I said above, I fixed my issue with: https://stackoverflow.com/questions/23437830/undefined-method-attr-accessible – Robert Vunabandi Jun 14 '20 at 18:13
3

To get around that problem I did this:

  1. First I check if the table exists.
  2. Then I check if my record exists.
  3. Otherwise I just set a default value.

It's kind of a hack but it works for my use case.

if ActiveRecord::Base.connection.table_exists? :settings # check if table exists
  if Setting.first.present? # check if first record has been seeded
    NUMBERS = Setting.first.my_numbers.split(",")
  else
    NUMBERS = '987654321'
  end
else
  NUMBERS = '123456789'
end
danielsmith1789
  • 1,056
  • 1
  • 12
  • 25
1

Above mentioned solutions would still fail on rake db:migrate or rake db:bootstrap if the database does not exist. In my case the build was getting failed on circleCI.

Following solution worked out for me properly.

# /app/config/initializers/settings.rb
db_loaded = ::ActiveRecord::Base.connection_pool.with_connection(&:active?) rescue false

if db_loaded && Setting.table_exists?
// business logic
end
-1

In the rails, wherever you are, you can interact with Models. Like

Setting.method_name()

Inside the rails application, you can create your own new folder(expect default directories created by rails new command) and can interact with Model methods.

Sourabh Upadhyay
  • 1,034
  • 11
  • 31