views:

132

answers:

1

Hi I have no idea why I am having problems with something so simple but this condition statement is just not working.

@battalion = Battalion.find(params[:id])
@soldier = @battalion.soldiers(:conditions => ["seniorleader = ?", "No"])

This shows the array of Soldiers but doesn't reflect the condition I have set.

I am sure I am doing something really simple wrong but I can't seem to figure out what it is.

Any help would be greatly appreciated.

+3  A: 

Based on the syntax you have in line two, you're passing in conditions as a property of soldiers, and not actually paring down your list. You may try:

@soldier = @battalion.soldiers.find(:all, :conditions => ["seniorleader = ?", "No"])

The find method will help you to sort through your soldiers, i would also recommend you do some eager loading on your first line,

@battalion = Battalion.find(params[:id], :include => "soldiers")

So the @battalion object will already have all the soldiers information, and will not require a separate sql query for line two. Hope this helps.

ThinkBohemian
Thanks a lot this solved it. I knew it was something stupid simple. Appreciate your time.
No, problem...been there done that. If you wanted to, you could do all of that in one line, check out this rails casts that has some more info http://railscasts.com/episodes/181-include-vs-joins .
ThinkBohemian