开发者

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 g开发者_运维百科reatly appreciated.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜