6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

rails c を行い、コンソールでモデル名.createでテーブルに保存がされていました。

2.3.0 :001> Blog.create(title: 'タイトルA', content: '内容A')
   (0.2ms)BEGIN
SQL (0.8ms)INSERTINTO "blogs" ("title", "content", "created_at", "updated_at")VALUES ($1, $2, $3, $4) RETURNING "id"  [["title", "タイトルA"], ["content", "内容A"], ["created_at", "2017-07-17 05:44:06.209381"], ["updated_at", "2017-07-17 05:44:06.209381"]]
   (3.0ms)COMMIT
=>#<Blog id: 1, title: "タイトルA", content: "内容A", created_at: "2017-07-17 05:44:06", updated_at: "2017-07-17 05:44:06">

Blog.create(title: 'タイトルA', content: '内容A')をすると、
上記を見ると自動的に事前に作った、blogsテーブルに保存がされています。
モデル名(Blog)とテーブル名(blogs)が微妙に違うのになぜ保存されるのだろうと思いました。
調べたところ、railsの命名規則に基づいて、テーブル名等を作ることによって、
railsが保存先を判断してくれるそうです。
命名規則の理由がわかりました。

6
2
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
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?