2

Sequel gem には、save「レコードを作成または更新する」メソッドがあります。新しいレコードが存在しない場合に新しいレコードを作成したり、既存のレコードのフィールドを更新したりするために使用しようとしています:

record = Records.where(:name => 'Dark Side of the Moon').first
record.save(:artist => "Pink Floyd")

Records.where(:name => 'Dark Side of the Moon')空の Record オブジェクトではなく nil を返すため、これは失敗します。基本的に、私はこのメソッドを として使用していますが、メソッドはモデル インスタンス メソッドであるupsertため、この方法では機能しません。save

私の質問は 2 つあります: (1) を使用する正しい方法は何saveですか? (2) Sequel にはレコードを「アップサート」する方法がありますか?

4

2 に答える 2

3

You can only use Model#save if you have a model instance. You can do that by creating a new model instance if there isn't one already in the database (when nil is returned).

record = Records[:name => 'Dark Side of the Moon'] || Records.new(:name => 'Dark Side of the Moon')
record.save(:artist => "Pink Floyd")
于 2013-04-07T15:08:46.577 に答える