Last active
December 28, 2015 00:18
-
-
Save kidach1/7412041 to your computer and use it in GitHub Desktop.
Railsを支える基本概念の整理(RESTfulやリソースなど) ref: http://qiita.com/kidachi_/items/43e53811c12351915278
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rails generate scaffold User name:string email:string | |
$ rake db:migrate #マイグレーションファイルを実行(dbの作成) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rails generate scaffold Micropost content:string user_id:integer | |
$ rake db:migrate #マイグレーションファイルを実行(dbの作成) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rails console | |
#Userモデルから任意のデータ(今回はid=2)を抽出、@testuserに代入 | |
irb(main):006:0> @testuser = User.find(2) | |
=> #<User id: 2, name: "RailsBeginner", email: "rails@test.com", created_at: "2013-11-09 08:30:21", updated_at: "2013-11-09 08:30:21"> | |
#@testuserが持っている(=リレーションのある)micropostsデータを閲覧 | |
irb(main):007:0> @testuser.microposts | |
=> #<ActiveRecord::Associations::CollectionProxy [#<Micropost id: 2, content: "rails post test", user_id: 2, created_at: "2013-11-09 08:30:48", updated_at: "2013-11-09 08:30:48">]> | |
irb(main):008:0> exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ heroku create | |
$ git push heroku master | |
$ heroku run rake db:migrate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Micropost < ActiveRecord::Base | |
belongs_to :user | |
validates :content, length: { maximum: 140 } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DemoApp::Application.routes.draw do | |
resources :microposts | |
resources :users | |
~ | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :microposts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment