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.

Associations.rb throws exception

#801

Setup:

I am using rails & activescaffold. I have a table which has some HATBM associations. Activescaffold requests a count of records, the finder constructs count parameters which have both a :include, and a :join.

Problem:

The if statement below (#1938) within associations.rb throws an exception.

def aliased_table_name_for(name, suffix = nil) if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{name.downcase}\son}

The 'name' parameter is passed to this method as a symbol. Symbol.downcase blows up. Perhaps a to_s is called for??

Reported by Meech · August 11th, 2008 @ 07:06 PM

State: invalid
Milestone: 2.3.10
Assigned to: nobody
Importance: Low

Activity

  1. Tarmo Tänav
    Tarmo Tänav
    • State changed from new to incomplete

    Symbols working where strings are expected is indeed a difficult issue, in this case to me it seems the problem could instead be solved by converting :join_table to a string right when the association is being defined and that only if it is a Symbol, otherwise we risk accepting invalid values that can be successfully converted to a string (same problem with adding to_s to aliased_table_name_for).

    Please do look into writing a patch (with a test) for it.

    September 18th, 2008 @ 05:33 AM

  2. josh
    josh
    • No changes were found…

    December 20th, 2008 @ 04:44 PM

  3. Ryan Bigg
    Ryan Bigg

    Is this still an issue? The method does not convert the passed-in name to a string, perhaps it should.

    April 10th, 2010 @ 08:55 AM

  4. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  5. Jeroen
    Jeroen
    • Importance changed from to

    I just ran against this one too. If you pass in a symbol as join table, there is an exception thrown: undefined method downcase' for :products_languages:Symbol

    Basically if you change

    def aliased_table_name_for(name, suffix = nil) if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{name.downcase}\son
    

    to

    def aliased_table_name_for(name, suffix = nil) if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{name.to_s.downcase}\son
    

    it did the trick for me. (notice the .to_s between name and downcase)

    I did not have time yet to look more deeply into this, neither to write a patch and/or tests.

    July 6th, 2010 @ 10:31 AM

  6. Neeraj Singh
    Neeraj Singh

    This is from ActiveRecord test cases.

    class Comment < ActiveRecord::Base
      scope :limit_by, lambda {|l| limit(l) }
      scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'"
      scope :for_first_post, :conditions => { :post_id => 1 }
      scope :for_first_author,
                  :joins => :post,
                  :conditions => { "posts.author_id" => 1 }
    
    end
    
    Comment.for_first_post.for_first_author.all
    

    Notice that :joins is accepting table name as symbol. And this test passes successfully.

    @Jeroen : Can you provide more information about your model and query so that I could create a failing test case.

    thanks

    July 6th, 2010 @ 03:49 PM

  7. Jeroen
    Jeroen

    Hi Neeraj, below you can find the two models:

    class Product < ActiveRecord::Base
      set_primary_key :products_id
      has_and_belongs_to_many :languages, :join_table => :products_to_languages, :foreign_key => :products_id, :association_foreign_key => :products_languages_id, :conditions => {:languagenav_id => DVDPost.product_languages[I18n.locale.to_s]}
    end
    
    class Language < ActiveRecord::Base
      set_table_name :products_languages
      set_primary_key :languages_id
      has_and_belongs_to_many :products, :join_table => :products_to_languages, :foreign_key => :products_languages_id, :association_foreign_key => :products_id
    end
    

    It happens when you do a habtm with a custom join table. These models are mapped to a (nasty) legacy db.
    Once I would change the join tables to a string instead of a symbol my problem was fixed. As you can see in my previous post I tracked down the exeption and it’s basically downcase that gets called on a symbol which doesn’t exist.

    It happens on here

    It seems that this issue has been resolved in Rails 3. At least this code has been refactored.

    July 30th, 2010 @ 01:23 PM

  8. Neeraj Singh
    Neeraj Singh
    • State changed from incomplete to open
    • Tag set to activerecord
    • Milestone changed from 3.x to 2.3.9
    • Importance changed from to Low

    July 30th, 2010 @ 01:45 PM

  9. Jeroen
    Jeroen
    • Tag cleared.

    Okay, this is odd. I've been trying for more than half an hour to reproduce it but I'm not able to do so anymore.

    I'll report back if I ever encounter it again.

    July 30th, 2010 @ 02:07 PM

  10. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.9 to 2.3.10

    August 30th, 2010 @ 02:28 AM

  11. Rohit Arondekar
    Rohit Arondekar
    • State changed from open to invalid

    Jeroen, feel free to leave a comment or a patch if you encounter the issue again. Closing for now.

    October 15th, 2010 @ 11:32 AM

  12. Jeff Kreeftmeijer