This project is archived and is in readonly mode.

#2282 ✓stale
John Wulff

has_many association_condition option

Reported by John Wulff | March 17th, 2009 @ 11:51 PM | in 3.x

I've added a new option to has_many association: association_condition allowing default "primary_key = foreign_key" condition to be overridden to something more complex like "primary_key LIKE foreign_key" or "SQL_FUNCTION(primary_key, foreign_key)"

This improvement allows for very complex associations to be handled cleanly.

Comments and changes to this ticket

  • Gravis

    Gravis April 26th, 2009 @ 11:45 PM

    I would +1 for a :joins options, allowing to specify the joining conditions on a has_many assoc (or habtm).

    I have a +/- complex request forcing me to use a finder_sql because of that. More info here : http://stackoverflow.com/questio... :

    [copy/paste:] " Hi,

    I have a relationship table :

    create_table "animal_friends", :force => true do |t|
    t.integer  "animal_id"
    t.integer  "animal_friend_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "status_id",        :default => 1
    end
    
    

    linking animals to others. Best way to retreive associations in SQL is :

    SELECT animals.*
    from animals join animal_friends as af 
    on animals.id = 
    case when af.animal_id = #{id} then af.animal_friend_id else
    af.animal_id end 
    WHERE #{id} in (af.animal_id, af.animal_friend_id)
    
    

    And I can't find a way to create a proper has_many relation in rails with this. Apparently, there's no way to provide joining conditions for has_many.

    I'm currently using a finder_sql :

    has_many :friends, :class_name => "Animal", :finder_sql => 'SELECT animals.* from animals join animal_friends as af on animals.id = case when af.animal_id = #{id} then af.animal_friend_id else af.animal_id end ' + 'WHERE #{id} in (af.animal_id, af.animal_friend_id) and status_id = #{Status::CONFIRMED.id}'

    but this method has the great disadvantage of breaking activerecord magic. For instance :

    @animal.friends.first
    
    

    will execute the finder_sql without limit, fetching thousands of rows, then taking the first of the array (and loosing several precious seconds / req).

    I guess it's a missing feature from AR, but I'd like to be sure first :) Thanks "

  • Jeremy Kemper

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

    • Milestone changed from 2.x to 3.x
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:53 PM

    • State changed from “new” to “open”
    • Importance changed from “” to “”

    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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:53 PM

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

Attachments

Pages