Skip to content

Instantly share code, notes, and snippets.

@kidach1
Last active December 28, 2015 00:18
Show Gist options
  • Save kidach1/7412041 to your computer and use it in GitHub Desktop.
Save kidach1/7412041 to your computer and use it in GitHub Desktop.
Railsを支える基本概念の整理(RESTfulやリソースなど) ref: http://qiita.com/kidachi_/items/43e53811c12351915278
$ rails generate scaffold User name:string email:string
$ rake db:migrate #マイグレーションファイルを実行(dbの作成)
$ rails generate scaffold Micropost content:string user_id:integer
$ rake db:migrate #マイグレーションファイルを実行(dbの作成)
$ 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
$ heroku create
$ git push heroku master
$ heroku run rake db:migrate
class Micropost < ActiveRecord::Base
belongs_to :user
validates :content, length: { maximum: 140 }
end
DemoApp::Application.routes.draw do
resources :microposts
resources :users
~
end
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