Ruby on Rails Tuesday, April 27, 2010

So one problem I've run into is that I cannot join beyond the table that
has been joined twice, without excessive amounts of SQL. For instance,
if there is a materials table (cards are made out of some material)

-----------------------------
Material < ActiveRecord::Base
has_many :cards
end
----------------------------

Then how can I query for decks that have aces and kings where they are
both made of cardboard. Since we have named each of the cards by name
(i.e. c1 and c2), I cannot refer further through the relations in the
database.

So the query would become:

---------------------------
Deck.all(
:joins =>
'inner join cards c1 on c1.deck_id=decks.id
inner join cards c2 on c2.deck_id=decks.id
inner join materials m on cards.material_id=c1.id
:conditions => ["c1.rank = 'Ace' AND c2.rank = 'King' and m.name =
'cardboard'])
---------------------------

Because I can't use rails-type associations to define the relations, it
can rapidly get out of hand. Right now I need to join across 4 relations
after the parallel join. Any ideas other than getting over it and
writing the SQL myself?

Thanks,
ben
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment