Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

habtm join requires an active connection

#6233

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.

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

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Russell Garner
    Russell Garner

    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})'
    

    January 17th, 2011 @ 10:36 AM

  2. Russell Garner
    Russell Garner

    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 :)

    January 17th, 2011 @ 10:58 AM

  3. Russell Garner
    Russell Garner

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

    January 31st, 2011 @ 05:57 PM

  4. Russell Garner
    Russell Garner

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

    January 31st, 2011 @ 06:02 PM

  5. rails
    rails
    • 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.

    May 1st, 2011 @ 01:00 AM

  6. rails
    rails
    • State changed from open to stale

    May 1st, 2011 @ 01:00 AM