Commit 0d77b39b by tady

post_tags, author associations

parent ed1d811a
class Post < ActiveRecord::Base
has_many :post_tags
has_many :tags, through: :post_tags
belongs_to :author, class_name: 'User'
end
class PostTag < ActiveRecord::Base
belongs_to :post
belongs_to :tag
end
class Tag < ActiveRecord::Base
has_many :post_tags
has_many :posts, through: :post_tags
end
require 'pp'
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
......@@ -7,14 +5,13 @@ class User < ActiveRecord::Base
:recoverable, :rememberable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:google_oauth2]
has_many :posts
# Device
def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
data = access_token.info
user = User.where(:email => data["email"]).first
pp data
unless user
user = User.create(name: data["name"],
image_url: data["image"],
......
<div class="text-box title"><%= @post.title %></div>
<div class="text-box meta">
<span class="label label-info">#Rails (4.x.x)</span>
<span class="label label-info">#ruby</span>
<span class="label label-info">#日報</span>
<span class="label label-success">@tady</span>
<% @post.tags.each do |tag| %>
<span class="label label-info">#<%= tag.name %></span>
<% end %>
<span class="label label-success">@<%= @post.author.name %></span>
<span class="label label-danger"><%= @post.updated_at.strftime('%Y-%m-%d') %></span>
</div>
<div class="text-box body"><%= h_application_format_markdown(@post.body) %></div>
......
......@@ -3,6 +3,7 @@ class CreatePosts < ActiveRecord::Migration
create_table :posts do |t|
t.string :title
t.text :body
t.integer :author_id
t.timestamps
end
......
class CreatePostTag < ActiveRecord::Migration
def change
create_table :post_tags do |t|
t.integer :post_id, null: false
t.integer :tag_id, null: false
t.timestamps
end
add_index :post_tags, :post_id
add_index :post_tags, :tag_id
end
end
......@@ -11,11 +11,22 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20131130112531) do
ActiveRecord::Schema.define(version: 20131130184007) do
create_table "post_tags", force: true do |t|
t.integer "post_id", null: false
t.integer "tag_id", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "post_tags", ["post_id"], name: "index_post_tags_on_post_id"
add_index "post_tags", ["tag_id"], name: "index_post_tags_on_tag_id"
create_table "posts", force: true do |t|
t.string "title"
t.text "body"
t.integer "author_id"
t.datetime "created_at"
t.datetime "updated_at"
end
......
......@@ -7,17 +7,34 @@
# Mayor.create(name: 'Emanuel', city: cities.first)
# Tag
tags = []
%w(JavaScript HTML CSS iPhone PHP Ruby Android Qiita Java Objective-C Vim Python ShellScript C++ C CoffeeScript Emacs git Perl jQuery
).each do |tag|
puts "[Seed Tag] #{tag}"
Tag.find_or_create_by(name: tag)
tags << Tag.find_or_create_by(name: tag)
end
# User
user = User.find_or_initialize_by(name: '山田太郎')
user.update_attributes!(email: "#{Devise.friendly_token[0,20]}@example.com", password: Devise.friendly_token[0,20])
user = User.find_or_initialize_by(name: '鈴木二郎')
user.update_attributes!(email: "#{Devise.friendly_token[0,20]}@example.com", password: Devise.friendly_token[0,20])
user = User.find_or_initialize_by(name: '田中三郎')
user.update_attributes!(email: "#{Devise.friendly_token[0,20]}@example.com", password: Devise.friendly_token[0,20])
# Post
users = User.all
Dir.glob(Rails.root.join('db', 'seeds').to_s + '/*').each do |file_name|
puts "[Post Tag] #{file_name}"
title = file_name.split('/').last
Post.find_or_create_by(title: title) do |post|
post = Post.find_or_initialize_by(title: title)
post.body = File.read(file_name)
end
post.author = users.sample
post.tags = [tags.sample, tags.sample]
post.save!
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment