📄 17 - adding taggability with a database mixin.rb
字号:
require 'cookbook_dbconnect'require 'og'require 'glue/taggable'class BlogPost is Taggable property :title, :content, Stringendog_connect# Now we can play around with tags.post = BlogPost.newpost.title = 'Some more facts about video games'post.tag(['editorial', 'games'])BlogPost.find_with_tags('games').each { |puts| puts post.title }# Some more facts about video gamesTag.find_by_name('editorial').blog_posts.each { |post| puts post.title }# Some more facts about video games#---DROP TABLE IF EXISTS tags;CREATE TABLE tags ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(32), PRIMARY KEY (id)) ENGINE=InnoDB;DROP TABLE IF EXISTS tags_blog_posts;CREATE TABLE tags_blog_posts ( tag_id INT(11), blog_post_id INT(11)) ENGINE=InnoDB;#---require 'cookbook_dbconnect'require 'taggable'activerecord_connectclass Tag < ActiveRecord::Baseendclass BlogPost < ActiveRecord::Base acts_as_taggableend# Now we can play around with tags.post = BlogPost.create(:title => 'Some more facts about inflation.')post.tag(['editorial', 'economics'])BlogPost.find_tagged_with(:any=>'editorial').each { |post| puts post.title }# Some more facts about inflation.#---DROP TABLE IF EXISTS tags_blog_posts;CREATE TABLE tags_blog_posts ( tag_id INT(11), blog_post_id INT(11), created_by_id INT(11)) ENGINE=InnoDB;#---require 'cookbook_dbconnect'require 'taggable'activerecord_connectclass Tag < ActiveRecord::Baseendclass Person < ActiveRecord::Baseend#---# ActiveRecord will automatically define the TagBlogPost class when# we reference it.class BlogPost < ActiveRecord::Base acts_as_taggable :join_class_name => 'TagBlogPost'end#---# Specify that a TagBlogPost is associated with a specific user.class TagBlogPost belongs_to :created_by, :class_name => 'Person', :foreign_key => 'created_by_id'end#---post = BlogPost.create(:title => 'My visit to the steel mill.')alice = Person.create(:name=>"Alice")post.tag(['travelogue', 'metal', 'interesting'], :attributes => { :created_by => alice })alices_interests = BlogPost.find_tagged_with(:all => 'interesting', :condition => "tags_people.created_by_id = #{alice.id}")alices_interests.each { |article| puts article.title }# My visit to the steel mill.#---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -