LoginSignup
18
18

More than 5 years have passed since last update.

Rails4.0 Newcomer

Last updated at Posted at 2013-02-28

all => ActiveRecord::Relation

Task.all.class.name
=> "ActiveRecord::Relation"

where.not

Task.where.not(title: "test")
 # => Task Load (0.3ms)  SELECT "tasks".* FROM "tasks" WHERE ("tasks"."title" != 'test')

where!

tasks = Task.order(:created_at); tasks.where!(title: "test")
 # => Task Load (0.3ms)  SELECT "tasks".* FROM "tasks" WHERE "tasks"."title" = 'test' ORDER BY "tasks"."created_at" ASC

複数カラムpluck

Task.where(id: 1).pluck(:title, :content, :count)
   (0.2ms)  SELECT "tasks"."title", "tasks"."content", "tasks"."count" FROM "tasks" WHERE "tasks"."id" = 1
 # => [["テスト", "テストタスク", 1]]

update_attributes => update

Task.update_attributes(params[:user])

Task.update(params[:task])

order => :asc/:desc

Task.order(created_at: :desc).all
 # => SELECT "tasks".* FROM "tasks" ORDER BY "tasks"."created_at" DESC
18
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18