2010-08-04 9 views

답변

4

일괄 SQL 업데이트를 수행할지 또는 유효성 검사 및 일반 콜백 체인을 호출하는 각 모델에 대한 업데이트를 수행할지 여부에 따라 다릅니다.
당신이 개체를 인스턴스화하고 콜백 체인을 실행하려면이 수행

Model.find(:all, :conditions => ["recipient_id = ? and inbox_id = ? and status='unread'", current_user.id, @inbox.id]).each do |obj| 
    obj.update_attributes(...) 
end 

당신이 SQL 업데이트를 수행 할 경우

Model.update_all("attr1='something', attr2=true,...", ["recipient_id = ? and inbox_id = ? and status='unread'", current_user.id, @inbox.id]) 

하기 위해 조건의 배열 양식을 사용하십시오를 당신은 SQL을 올바르게 탈출 할 수 있습니다.

0
Model.update_all("foo='bar'", "recipient_id = #{current_user.id} and inbox_id = #{@inbox.id} and status='unread'") 
1

where 조건을 사용하여 대량 SQL 업데이트를 수행 할 수도 있습니다.이 경우 더 읽기 쉽습니다.

Model.where(recipient_id: current_user.id) 
    .where(inbox_id: @inbox.id) 
    .where(status: 'unread') 
    .update_all("attr1='something_new'") 
관련 문제