Paste
Pasted by registered user omemunama ( 10 years ago )
################## model ###############
class Term < ActiveRecord::Base
belongs_to :post
belongs_to :category
end
class Post < ActiveRecord::Base
belongs_to :user
has_many :terms
has_many :categories, :through => :terms
has_many :comments
def to_s
title
end
end
class Category < ActiveRecord::Base
has_many :posts
has_many :posts, :through => :terms
def to_s
title
end
end
################## views ###############
########## categories/show #############
<h1>Category <em><%= @category %></em></h1>
<section class="listing">
<%= render partial: 'posts/post', collection: @category.posts.order('created_at desc') %>
</section>
################## shema #######################
create_table "categories", force: :true do |t|
t.string "title"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "comments", force: :true do |t|
t.integer "post_id"
t.integer "user_id"
t.text "comment"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "posts", force: :true do |t|
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
end
create_table "terms", force: :true do |t|
t.integer "post_id"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :true do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
Revise this Paste
Parent: 79999