Commit 0d77b39b by tady

post_tags, author associations

parent ed1d811a
class Post < ActiveRecord::Base class Post < ActiveRecord::Base
has_many :post_tags
has_many :tags, through: :post_tags
belongs_to :author, class_name: 'User'
end end
class PostTag < ActiveRecord::Base
belongs_to :post
belongs_to :tag
end
class Tag < ActiveRecord::Base class Tag < ActiveRecord::Base
has_many :post_tags
has_many :posts, through: :post_tags
end end
require 'pp'
class User < ActiveRecord::Base class User < ActiveRecord::Base
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable # :confirmable, :lockable, :timeoutable and :omniauthable
...@@ -7,14 +5,13 @@ class User < ActiveRecord::Base ...@@ -7,14 +5,13 @@ class User < ActiveRecord::Base
:recoverable, :rememberable, :trackable, :validatable :recoverable, :rememberable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:google_oauth2] devise :omniauthable, :omniauth_providers => [:google_oauth2]
has_many :posts
# Device # Device
def self.find_for_google_oauth2(access_token, signed_in_resource=nil) def self.find_for_google_oauth2(access_token, signed_in_resource=nil)
data = access_token.info data = access_token.info
user = User.where(:email => data["email"]).first user = User.where(:email => data["email"]).first
pp data
unless user unless user
user = User.create(name: data["name"], user = User.create(name: data["name"],
image_url: data["image"], image_url: data["image"],
......
<div class="text-box title"><%= @post.title %></div> <div class="text-box title"><%= @post.title %></div>
<div class="text-box meta"> <div class="text-box meta">
<span class="label label-info">#Rails (4.x.x)</span> <% @post.tags.each do |tag| %>
<span class="label label-info">#ruby</span> <span class="label label-info">#<%= tag.name %></span>
<span class="label label-info">#日報</span> <% end %>
<span class="label label-success">@tady</span> <span class="label label-success">@<%= @post.author.name %></span>
<span class="label label-danger"><%= @post.updated_at.strftime('%Y-%m-%d') %></span> <span class="label label-danger"><%= @post.updated_at.strftime('%Y-%m-%d') %></span>
</div> </div>
<div class="text-box body"><%= h_application_format_markdown(@post.body) %></div> <div class="text-box body"><%= h_application_format_markdown(@post.body) %></div>
......
...@@ -3,6 +3,7 @@ class CreatePosts < ActiveRecord::Migration ...@@ -3,6 +3,7 @@ class CreatePosts < ActiveRecord::Migration
create_table :posts do |t| create_table :posts do |t|
t.string :title t.string :title
t.text :body t.text :body
t.integer :author_id
t.timestamps t.timestamps
end 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 @@ ...@@ -11,11 +11,22 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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| create_table "posts", force: true do |t|
t.string "title" t.string "title"
t.text "body" t.text "body"
t.integer "author_id"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
end end
......
...@@ -7,17 +7,34 @@ ...@@ -7,17 +7,34 @@
# Mayor.create(name: 'Emanuel', city: cities.first) # 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 %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| ).each do |tag|
puts "[Seed Tag] #{tag}" puts "[Seed Tag] #{tag}"
Tag.find_or_create_by(name: tag) tags << Tag.find_or_create_by(name: tag)
end 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| Dir.glob(Rails.root.join('db', 'seeds').to_s + '/*').each do |file_name|
puts "[Post Tag] #{file_name}" puts "[Post Tag] #{file_name}"
title = file_name.split('/').last title = file_name.split('/').last
Post.find_or_create_by(title: title) do |post|
post.body = File.read(file_name) post = Post.find_or_initialize_by(title: title)
end post.body = File.read(file_name)
post.author = users.sample
post.tags = [tags.sample, tags.sample]
post.save!
end 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