Lauren
Lauren

Reputation:

Rails Condition Statement Issue

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.

Upvotes: 0

Views: 396

Answers (1)

Schneems
Schneems

Reputation: 15858

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.

Upvotes: 3

Related Questions