Since you are going to join the same table twice, you might as well
write the whole SQL manually. ActiveRecord doesnt allow you to do
something like :joins => [:cards as 'c1', :cards as 'c2'].
Since you are going to join the same table twice, you might as well
write the whole SQL manually. ActiveRecord doesnt allow you to do
something like :joins => [:cards as 'c1', :cards as 'c2'].
ok. That's not a very satisfying answer but it is good to be confident
that I'm not missing something obvious.
Obviously I'm interested if anyone can improve on Sharagoz's comment..
If you just want to use activerecord without writing inner joins
manually you can do
@decks_with_aces = Deck.all(:joins => :cards, :conditions =>
"cards.rank = ''Ace")
@decks_with_kings = Deck.all(:joins => :cards, :conditions =>
"cards.rank = ''King")
@decks_with_aces_and_kings = @decks_with_aces & @decks_with_kings
That basically puts all decks with kings in an array, the same for
aces, and then uses the arrays & method to find elements common in
both.
It's not perfect performance wise, but it works
Since you are going to join the same table twice, you might as well
write the whole SQL manually. ActiveRecord doesnt allow you to do
something like :joins => [:cards as 'c1', :cards as 'c2'].
Except that it does, since :joins can take a string.
Since you are going to join the same table twice, you might as well
write the whole SQL manually. ActiveRecord doesnt allow you to do
something like :joins => [:cards as 'c1', :cards as 'c2'].
Except that it does, since :joins can take a string.
I'm unsure whether Sharagoz was referring to was writing a find_by_sql
statement or merely the 'whole' of the :joins string. Unless there is
something I'm missing, I don't think writing out a :joins SQL string is
the solution here.
If you just want to use activerecord without writing inner joins
manually you can do
@decks_with_aces = Deck.all(:joins => :cards, :conditions =>
"cards.rank = ''Ace")
@decks_with_kings = Deck.all(:joins => :cards, :conditions =>
"cards.rank = ''King")
@decks_with_aces_and_kings = @decks_with_aces & @decks_with_kings
That basically puts all decks with kings in an array, the same for
aces, and then uses the arrays & method to find elements common in
both.
It's not perfect performance wise, but it works
Thanks, but as you suggest the performance cost is prohibitive for me.
ben
Yes, I ment writing the :joins part of the SQL manually. I think
that's the closest you're going to get.
If activerecord had supported what you're looking for I believe
someone would have alerted us to this by now.