When I invoke find() method on a model (MyModel.find(:all, :conditions => ‘…’)), I do not have to pass a database connection as a parameter.
How does this work? What technique does Rails use to make this possible? Does it use dependency injection?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The database connection is defined in database.yml. During startup – Rails passes the connection to ActiveRecord::Base. As all the models are inheriting from ActiveRecord::Base they become ‘aware’ of what connection to use.
Take a look at http://api.rubyonrails.org/classes/ActiveRecord/Base.html