views:

53

answers:

1

I have two models: CreditCard and BlacklistItem::CreditCard. If I search for a BlacklistItem::CreditCard first, I get the expected behaviour:

>> BlacklistItem::CreditCard.find(:all).first
=> #<BlacklistItem::CreditCard id: 5, *snip* >
>>

If I search for a CreditCard first, when I go to look for BlacklistItem::CreditCard items later I get unexpected behaviour:

>> CreditCard.find(:all).first
=> #<CreditCard id: 2, key_id: 4, *snip* >
>> BlacklistItem::CreditCard.find(:all).first
(irb):2: warning: toplevel constant CreditCard referenced by BlacklistItem::CreditCard
=> #<CreditCard id: 2, key_id: 4, *snip* >
>>

What am I doing wrong? Is it just impossible to have names with this kind of relationship between them? I'm going to rename BlacklistItem::CreditCard as a work around, but it really would be the best name for this particular object.

+6  A: 

Namespaced models in Rails have been buggy for quite a while. There's a good writeup on the matter here. You might try explicitly specifying the table name to use for blacklisted credit cards in the model via:

class BlacklistItem::CreditCard
  set_table_name :blacklist_item_credit_card

  ...

end

However, even with this, having a top level CreditCard model and namespaced one, BlacklistItem::CreditCard, you may still run into problems due to Rails' automagic behavior.

Ian