update_allメソッドって便利だなー
where()などで必要な複数レコードを抽出しておいて、一括で値変更できます。
(ActiveRecord::Relationのメソッドとして実装されてる)
いままでsql直書きしてたよ。。(´・ω・`)
その前は、eachをレコード分実行して値書き換えてたよ。よくないよね。。(´;ω;ω;`)
忘備録的にリファレンスマニュアルのサンプルを貼り付けます。
# Update all customers with the given attributes Customer.update_all :wants_email => true # Update all books with 'Rails' in their title Book.update_all "author = 'David'", "title LIKE '%Rails%'" # Update all avatars migrated more than a week ago Avatar.update_all ['migrated_at = ?', Time.now.utc], ['migrated_at > ?', 1.week.ago] # Update all books that match conditions, but limit it to 5 ordered by date Book.update_all "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5 # Conditions from the current relation also works Book.where('title LIKE ?', '%Rails%').update_all(:author => 'David') # The same idea applies to limit and order Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(:author => 'David')
【Rails3.2】uniqメソッドを使った際に、ActiveRecord::StatementInvalidが発生してしまう場合
Rails3.2から導入されたActiveRecord::Relation#uniq はとっても便利で、
records.select(:name).uniq
と書くだけで、ユニークなnameの一覧が取得できます!
(recordsは何かしらレコードの集合です。)
でも
records = Client.order(:address) : records.select(:name).uniq
と書くと、
ActiveRecord::StatementInvalid: PG::Error: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
というようなエラーを吐いてしまいます。
これは、selectしていないカラムに含まれていないカラム(この例ではaddress)でorderしようとしたために発生しています。
この例ではorderを消せば良い話ですが、
他のメソッドからorder済みの値が渡ってくる場合は困ってしまいます。(´・ω・`)
そんな際は、reorder()メソッドを使えば良いです。
records = Client.order(:address) : records.reorder(:name) #それまでのorderをなかったことにして、orderしなおす records.select(:name).uniq
以上により、selectに含まれないカラムに対するorderを消すことが出来、無事動くようになりました。