LoginSignup
0
0

More than 5 years have passed since last update.

ActiveRecord::Baseメソッドの基本中の基本

Last updated at Posted at 2016-08-26

Tweetモデルがありtweetsteテーブルがある場合。。。

Tweet.all
Tweet.new
Tweet.save

Tweet.create
Tweet.create(name: "takashi", text: "Nice to meet you!")

Tweet.find
tweet = Tweet.find(1)
#tweetにtweetsテーブルのID1の情報が代入されている状態。
Tweet.find(1)でももちろんOK

Tweet.find_by(name: 'tanaka')
とするとIDじゃなくても検索できるよ

Tweet.where(name: 'atsushi')
nameのカラムがatsushiのものを全部引っ張ってくる。

Tweet.limit(5)
tweetsテーブルから最初の5件を取得(IDが若い順)

tweets = Tweet.all
tweets.order('id DESC')
指定したレコードのカラムの中身の数値が大きい順にレコードを並び替え
つまりIDが大きい順に並ぶから最新のものから出るという事
DESC は降順の意味の Descending、ASC は昇順の意味の Ascending という英語の略です。

tweet = Tweet.find(3)
tweet.update(name: 'takeshi')
で情報の更新を行う。

<%= simple_format(引数) %>
①改行は
を付与
②文字列を

で括る

0
0
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
0
0