4

Here are my models:

class Deck < ActiveRecord::Base
  belongs_to :game
  has_many :deck_cards
end

class DeckCard < ActiveRecord::Base
  belongs_to :card
  belongs_to :deck
end

class Card < ActiveRecord::Base
end

Here's my attempted find:

DeckCard.all :joins => [:card, :deck], :conditions => {{:decks => {:game_id => @game.id}}, {:cards => {:present => true}}}

I keep getting the error : undefined method for all for #Class:0x4b2a98>. I'm assuming this is a misleading error from parsing my conditions. I'm following the guide for Active Record Query. I wasn't sure about whether to use the singular or plural form of the associations. Look like with a belongs_to, you're supposed to use singular form in the :joins hash, but I wasn't sure in the :conditions hash, so I tried both and neither worked.

In case it isn't clear, what I'm trying to do in SQL is:

SELECT * from DeckCards  
INNER JOIN decks on decks.id = deck_cards.deck_id  
INNER JOIN cards on card.id = deck_cards.card_id  
WHERE decks.game_id = 4  
AND cards.present = true

I'm able to get around it for now by using DeckCard.find_by_sql, but it would be nice to figure out why the joins and conditions on associations isn't working.

I'm using InstantRails-2.0 on windows, which is using Rails 2.0.2

Edited : some progress using DeckCard.find(:all ...) instead. I also edited the brackets based on another answer. My latest code is

DeckCard.find :all, :joins => [:card, :deck], :conditions => {:deck => {:game_id => @game.id}, :cards => {:present => true}}  

which is producing the following error:

Unknown column 'deck_cards.decks' in 'where clause': SELECT `deck_cards`.* FROM `deck_cards`   INNER JOIN `cards` ON `cards`.id = `deck_cards`.card_id  INNER JOIN `decks` ON `decks`.id = `deck_cards`.deck_id  WHERE (`deck_cards`.`decks` = '--- \n- :game_id\n- 5\n' AND `deck_cards`.`cards` = '--- \n- :present\n- true\n')  

The joins appear correct but not the WHERE conditions. I've tried a few different things like :deck or :decks in the conditions clause but no luck. Could this be another difference between the current ActiveRecord Query Interface docs and how conditions are done in 2.0.2?

Thanks!

user26270
  • 6,904
  • 13
  • 62
  • 94
  • This still strikes me as a syntax error, seeing as it won't even identify the class DeckCard by name. Can you post the surrounding method for the DeckCard.all line? – Pesto Mar 10 '09 at 00:12
  • See my updated answer for another suggestion. – Sarah Mei Mar 11 '09 at 03:15

6 Answers6

3

You need to complete your association with the Card model:

class Card < ActiveRecord::Base
  has_many :deck_cards
end

EDIT 2: Try this:

 DeckCard.find :all, :joins => [:card, :deck], :conditions => ["decks.game_id = ? and cards.present = ?", @game.id, true]
Sarah Mei
  • 18,154
  • 5
  • 45
  • 45
1

Your :conditions contains 2 hashes. This is incorrect. You should have two keys (:decks and :cards) which should each have a hash as a value. Correct yours to look like this:

:conditions => {:decks => {:game_id => @game.id}, :cards => {:present => true}}
Pesto
  • 23,810
  • 2
  • 71
  • 76
1

I didn't test this, but what happens if you use...

DeckCard.find(:all, :include => [:cards, :deck], :conditions => {:deck => {:game_id => @game.id}, :cards => {:present => true}})
danengle
  • 566
  • 4
  • 9
  • this is getting closer; the .find(:all...) is the right method for 2.0.2; I can now see the SQL generated; the JOINs are right, the WHERE clause conditions are not; I'm running out of space, have to show error in another comment or in edited post above – user26270 Mar 10 '09 at 20:58
1

What version of rails? ActiveRecord#all was added sometime after 2.0.2.

What does a puts DeckCard.respond_to?(:all) result in?

wombleton
  • 8,336
  • 1
  • 28
  • 30
  • Sorry, I forgot to mention that I'm using InstantRails-2.0, which has Rails 2.0.2; I haven't updated it to the latest Rails (2.3?); DeckCard.respond_to?(:all) returns false; I guess the compiler was right, with the "undefined method for 'all'"! so what should I be using in 2.0.2? or upgrade? – user26270 Mar 10 '09 at 01:54
  • For 2.0.2 use find(:all, ...blahblah...) as @danengle mentioned, I believe. If you can flip to 2.2.2, I'd do that though. – wombleton Mar 10 '09 at 03:14
0

Your syntax is also backwards. I believe the join (and join table) needs to be arranged alphabetically. Hence card_decks. I believe this is the default configuration of rails. I had a problem similar to this once before I started using has_many => :through

stivlo
  • 83,644
  • 31
  • 142
  • 199
0

@game.deck.deckcards.joins(:cards).where('cards.present' => true)

rails 4 syntax is much better

Sam G
  • 483
  • 4
  • 4