目前在 app/models 裡
有 bang.rb、commnet.rb、girb.rb
想看這些 model 的 schema 資訊
都要跑到 db/schema.rb 去看
其實不怎麼方便
這裡來介紹一個好用的 gem
annotate
到 Gemfile 去加入
Gemfile
source 'https://rubygems.org'
gem 'faker'
gem 'font-awesome-sass', '~> 4.2.0'
gem 'bootstrap-sass'
gem 'annotate'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
記得 bundle
bundle
重啓 server
rails s
在 terminal 鍵入 annotate
annotate
成功的話會出現 Annotated (3): Bang, Comment, Girb 這樣的訊息
接著去看 bang.rb、comment.rb、girb.rb
可以發現在最上頭多了一些#字號的東西出來
app/models/bang.rb
# == Schema Information
#
# Table name: bangs
#
# id :integer not null, primary key
# girb_id :integer
# created_at :datetime
# updated_at :datetime
#
class Bang < ActiveRecord::Base
belongs_to :girb
end
app/models/comment.rb
# == Schema Information
#
# Table name: comments
#
# id :integer not null, primary key
# commenter :string(255)
# body :text
# girb_id :integer
# created_at :datetime
# updated_at :datetime
#
class Comment < ActiveRecord::Base
belongs_to :girb
end
app/models/girb.rb
# == Schema Information
#
# Table name: girbs
#
# id :integer not null, primary key
# name :string(255)
# created_at :datetime
# updated_at :datetime
# like :integer default(0)
# email :string(255)
#
class Girb < ActiveRecord::Base
has_many :bangs, dependent: :destroy
has_many :comments, dependent: :destroy
def self.search(query)
where("name like ?", "%#{query}%")
end
end