This project is archived and is in readonly mode.

#6233 ✓stale
Nathan Witmer

habtm join requires an active connection

Reported by Nathan Witmer | December 30th, 2010 @ 01:27 AM

ActiveRecord::Associations#create_has_and_belongs_to_many_reflection, currently line 1814 of associations.rb, accesses AR::Base's connection in order to do some sanity checking on the association. Unfortunately, this assumes a connection is established and active when the data model is being defined, and causes an ActiveRecord::ConnectionNotEstablished exception when defining a data model without having connected. This prevents anyone from defining has_and_belongs_to_many relationships at load time.

I'm not sure what the best approach to fix this is. The sanity checking makes sense, but the assumption that the connection is there doesn't.

Comments and changes to this ticket

  • Russell Garner

    Russell Garner January 17th, 2011 @ 10:36 AM

    I'm seeing this too - association declaration below. Is there a recommended workaround?

            has_and_belongs_to_many :related_tags, :class_name => 'SemanticallyTaggable::Tag',
                                    :association_foreign_key => 'related_tag_id', :join_table => 'related_tags',
                                    :insert_sql => 'INSERT INTO related_tags (`tag_id`, `related_tag_id`) VALUES (#{id}, #{record.id}), (#{record.id}, #{id})',
                                    :delete_sql => 'DELETE FROM related_tags WHERE (tag_id = #{id} AND related_tag_id = #{record.id}) OR (tag_id = #{record.id} AND related_tag_id = #{id})'
    
  • Russell Garner

    Russell Garner January 17th, 2011 @ 10:58 AM

    Here's my (not very good) workaround:

    after "require "rails/all" in application.rb

      database_yml = File.expand_path('../database.yml', __FILE__)
      if File.exists?(database_yml)
        active_record_configuration = YAML.load_file(database_yml)[Rails.env]
        ActiveRecord::Base.establish_connection(active_record_configuration)
      else
        raise "Please create #{database_yml} first to configure your database."
      end
    

    However, the habtm in question is being defined in a gem, which makes it harder than it needs to be for people to add a reference to the gem. Still open to much better suggestions than mine :)

  • Russell Garner

    Russell Garner January 31st, 2011 @ 05:57 PM

    Ok, a quick autoload on the offending class also works very well in a Railtie and obviates the need for the above hackery :)

  • Russell Garner

    Russell Garner January 31st, 2011 @ 06:02 PM

    Don't know why I mentioned a Railtie there. One is not required...

  • rails

    rails May 1st, 2011 @ 01:00 AM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • rails

    rails May 1st, 2011 @ 01:00 AM

    • State changed from “open” to “stale”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages