0

我在“书”模型中有以下命名范围

named_scope :batch_book_status_55, lambda { |batch_id| {
:select => "ifnull( sum( if( BK_DB_STATUS_FK = 55, 1, 0 ) ), 0) as ASSIGNED",
:conditions => ["BK_BATCH = ?", batch_id]
}}



<%  Book.batch_book_status_55(batch.BATCH_ID).each_with_index do |book, index| %>
  <% if book.ASSIGNED.to_i != 0%>
  Assigned : <%= book.ASSIGNED %> 
  <% end %>
<% end %

下面的代码显示特定批次中状态 = 55 (BK_DB_STATUS_FK = 55) 的书籍数量。

我对命名范围代码有点困惑。如果有人可以向我解释代码,我将不胜感激。

4

1 回答 1

1

您最好在self此处使用方法而不是命名范围。请参考:http ://www.railway.at/2010/03/09/named-scopes-are-dead/

于 2013-05-08T11:58:18.873 回答