Just curious as to why this happens in the rails console:
Loading development environment (Rails 4.1.5)
2.1.2 :001 > require 'application_controller'
=> true
2.1.2 :002 > ApplicationController
=> ApplicationController
2.1.2 :003 > ApplicationController.subclasses
=> []
2.1.2 :004 > ::ApplicationController.descendants
=> []
I have this controller
class MyController < ApplicationController
end
So I'm expecting ApplicationController.subclasses #=> [MyController], however, I'm getting an empty array :(
Well, credit goes to MrYoshiji, but here's how you display subclasses.:
Loading development environment (Rails 4.1.5)
2.1.2 :001 > require 'application_controller'
=> true
2.1.2 :001 > require 'my_controller'
=> true
2.1.2 :002 > ApplicationController
=> ApplicationController
2.1.2 :003 > ApplicationController.subclasses
=> [MyController]
Lazy load means an object doesn't exist in memory until it is specifically called during runtime. My second command, require 'my_controller actively references a subclass of ApplicationController and so it appears when we list its subclasses with the .subclass method.
Related
I am opening rails console session and do:
2.6.3 :048 > ActiveRecord::Base.connected?
=> true
2.6.3 :049 > ActiveRecord::Base.connection_pool.disconnect!
=> []
2.6.3 :050 > ActiveRecord::Base.connected?
=> false
2.6.3 :051 > ActiveRecord::Base.establish_connection(:development)
=> #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x00 ... >
2.6.3 :052 > ActiveRecord::Base.connected?
=> false
2.6.3 :053 > SomeModel.connection
=> #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter:0x00 ...>
2.6.3 :055 > ActiveRecord::Base.connected?
=> true
$ rails -v
Rails 5.2.3
my config/database.yml file has to be fine, because HTTP requests are working fine.
Why I cant establish connection in rails console this way?
I am asking because I have similar use of establish_connection in one of config/initializers/ file, that is configuring sneakers workers like here and there it is also returning me false on ActiveRecord::Base.connected?
To connect again you can use
ActiveRecord::Base.connect
AR calls establish_connection only once, for ActiveRecord::Base. All subclasses use the one connection.
You can manually call establish connection yourself on some subclasses. This is very convenient for using two databases at once, e.g.
class MyMainUser < ActiveRecord::Base; end
class MyOtherDb < ActiveRecord::Base; end
class MyOtherUser < MyOtherDb; end
MyOtherDb.establish_connection ...
MyMainUser.first # uses default db
MyOtherUser.first # uses other db
You can't do queries that would cross databases though.
To connect you can use ActiveRecord::Base.connection and than you can call Somemodel.first and its should work.
I'm trying to connect to my remote mongoDB database through irb shell.
I ran the following:
2.3.0 :001 > require 'mongo'
=> true
2.3.0 :002 > Mongo::Logger.logger.level = ::Logger::INFO
=> 1
2.3.0 :003 > require 'uri'
=> false
The last statement should return true. I attempted to initialise the client after this via:
db = Mongo::Client.new(ENV['MLAB_URI'])
And got the error:
NoMethodError: undefined method `each' for nil:NilClass
I think you haven't set the ENV variable .
check ENV['MLAB_URI'] variable value.
This question already has an answer here:
Rails table_name_prefix is not working as expected
(1 answer)
Closed 1 year ago.
I have this code:
# app/models/ta.rb
module Ta
def self.table_name_prefix
'ta_'
end
end
...
# app/models/ta/article.rb
module Ta
class Article < ActiveRecord::Base
end
end
From the rails console...
# development environment
Loading development environment (Rails 4.1.6)
2.1.3 :001 > Ta::Article.table_name
=> "ta_articles"
2.1.3 :002 >
...
# production environment
Loading production environment (Rails 4.1.6)
2.1.3 :001 > Ta::Article.table_name
=> "articles"
2.1.3 :002 >
Why is this happening?
Add to config/initializers/namespace.rb something like:
require Rails.root.join('app', 'models', 'ta')
should solve your problem.
i have some problem with removing diacritics with iconv in ruby on rails
here is my code:
class Diacritics
def removeDiacritics(text)
dRemover = Iconv.new("ASCII//TRANSLIT", "UTF-8")
text = dRemover.iconv(text).gsub(/[^a-zA-Z0-9 ]/, '')
end
end
this is output:
1.9.3-p392 :001 > require "diacritics"
/usr/local/rvm/gems/ruby-1.9.3-p392#persoc/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `block in require': iconv will be deprecated in the future, use String#encode instead.
=> true
1.9.3-p392 :002 > remover = Diacritics.new
=> #<Diacritics:0x00000004237068>
1.9.3-p392 :003 > text = "Dánský prezídent"
=> "Dánský prezídent"
1.9.3-p392 :004 > remover.removeDiacritics(text)
=> "Dnsk prezdent"
i expect "Dansky prezident"
server apache on fedora (httpd), using rvm and ruby 1.9.3-p392
Can anybody help me?
You can use the ActiveSupport::Inflector.transliterate method.
ActiveSupport::Inflector.transliterate("Dánsky prezídent") # => "Dansky prezident"
If you need this for a url slug, it's even easier.
"Dánsky prezídent".parameterize # => "dansky-prezident"
I think I followed the description of how to make observers exactly, Model page:
class Page
include Mongoid::Document
field :title, type: String
field :content, type: String
end
I have an observer (app/observers/page_observer.rb):
class PageObserver < Mongoid::Observer
observe :page # just to be sure!
def initialize
puts "Page observer initialized"
end
def after_update page
puts "After update page "+page
end
end
I added it to config/application.rb:
config.mongoid.observers = :page_observer
Then when I do rails c, I get:
$ rails c
Page observer initialized
Loading development environment (Rails 3.2.13)
1.9.3p194 :005 > p = Page.first
=> #<Page _id: 5174ce01681167de23000001, title: "Hi", content: nil>
1.9.3p194 :006 > p.title = "Hi1"
=> "Hi1"
1.9.3p194 :007 > p.save
=> true
Shouldn't I have seen a "After update page " when I p.save?
Full code of this simplified example: https://github.com/dts/mongoid_observer_test
Rails Models are not loaded until they are needed, but you cannot register an observer on a class that hasn't been loaded yet, so you will need to forcibly load the Page class first. Try adding require File.expand_path('../../app/models/article', __FILE__) to application.rb after all the other files are loaded, i.e. after if defined?(Bundler) ... end