This project is archived and is in readonly mode.

#801 ✓invalid
Meech

Associations.rb throws exception

Reported by Meech | August 11th, 2008 @ 07:06 PM | in 2.3.10

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??

Comments and changes to this ticket

  • Tarmo Tänav

    Tarmo Tänav September 18th, 2008 @ 05:33 AM

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

  • josh

    josh December 20th, 2008 @ 04:44 PM

    • no changes were found...
  • Ryan Bigg

    Ryan Bigg April 10th, 2010 @ 08:55 AM

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

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Jeroen

    Jeroen July 6th, 2010 @ 10:31 AM

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

  • Neeraj Singh

    Neeraj Singh July 6th, 2010 @ 03:49 PM

    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

  • Jeroen

    Jeroen July 30th, 2010 @ 01:23 PM

    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.

  • Neeraj Singh

    Neeraj Singh July 30th, 2010 @ 01:45 PM

    • State changed from “incomplete” to “open”
    • Tag set to activerecord
    • Milestone changed from 3.x to 2.3.9
    • Importance changed from “” to “Low”
  • Jeroen

    Jeroen July 30th, 2010 @ 02:07 PM

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

  • Jeremy Kemper

    Jeremy Kemper August 30th, 2010 @ 02:28 AM

    • Milestone changed from 2.3.9 to 2.3.10
  • Rohit Arondekar

    Rohit Arondekar October 15th, 2010 @ 11:32 AM

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

  • Jeff Kreeftmeijer

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