How do i work with two different databases in rails with active records?

Add new sections to your database.yml e.g. other_development: adapter: mysql database: otherdb_development username: root password: host: localhost other_production: adapter: mysql database: otherdb_production username: root password: host: localhost Add a class in lib/other_database.rb class OtherDatabase < ActiveRecord::Base establish_connection “other_#{RAILS_ENV}” end and then for each model which isn’t in the default database subclass from OtherDatabase e.g.: class … Read more

Why polymorphic association doesn’t work for STI if type column of the polymorphic association doesn’t point to the base model of STI?

Good question. I had exactly the same problem using Rails 3.1. Looks like you can not do this, because it does not work. Probably it is an intended behavior. Apparently, using polymorphic associations in combination with Single Table Inheritance (STI) in Rails is a bit complicated. The current Rails documentation for Rails 3.2 gives this … Read more

Rails includes with scope

I think the best solution would be: Author.includes(:articles).where(:articles=>{published: true}).find(params[:author_id]) Or you can create scope: class Author < ActiveRecord::Base scope :with_published_articles, -> { includes(:articles).where(articles: { published: true}) } end and then: Author.with_published_articles.find(params[:author_id].to_s)

Rails: Is it bad to have an irreversible migration?

If you are dealing with production-grade systems then yes, it is very bad. If it is your own pet project, then anything is allowed (if nothing else, it will be a learning experience 🙂 though chances are that sooner rather than later, even in a pet project, you will find yourself having put a cross … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)