views:

64

answers:

1

Hello,

I have the following code in Rails:

@possibleMatchingOffers = SmsOffer.valid.find(:all, :conditions => {:hub_phone_no => unhndledMsg.hub_phone_no})
    @matchingContact = @possibleMatchingOffers.biz_sms_reply_queues.valid.find(:all)

The Error I'm getting:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

@possibleMatchingOffers is an array so it's not accepting to use the association (.biz_sms_reply_queues).

I can figure out manual way to do it but I was wondering if there is better easy way to do this.

Thanks,

Tam

A: 

Without knowing what you're intention is here, it looks like you need to turn the first "find" into an named scope. So it would look something like: SmsOffer.valid.by_hub_phone_no(unhndledMsg.hub_phone_no).biz_sms_reply_queues

Named scopes return AR Proxy objects, and thus, you can use an association on them.

What is "valid"? This isn't a rails method. Is it also a named scope? You should probably dry that up also.

BJ Clark
Thanks BJ..valid is actually a named scope but it's not the problem. What I want to do is to get related records for array of records. If I do something like:@possibleMatchingOffers = SmsOffer.valid.find(:first)@matchingContact = @possibleMatchingOffers.biz_sms_reply_queuesit works! but once I get an array of records for possibleMatchingOffers it doesn't work and I don't want to loop through the results to get them one by one so I was wondering if there is shortcut to do this in Rails
Tam
Can you post/tell me what biz_sms_reply_queues does? Does it want an array or an AR Proxy object?
BJ Clark