has_many polymorphic
来源:互联网 发布:免费门窗设计软件 编辑:程序博客网 时间:2024/06/04 20:07
To create a polymorphic has_many :through, you must first create your models. We will use'Article,' 'Category,' and 'Tag' where 'Tag' is the join-model and Article is one of many objects which can be "tagged" with a category.
First you create your 'Article' and 'Category' models. These are basic models which do not need any special attention, just yet:
rails g model Article name:stringrails g model Category name:string
Now, we will create our polymorphic join-table:
rails g model Tag taggable_id:integer taggable_type:string category_id:integer
The join-table joins together two tables, or in our case one table to many others via polymorphic behavior. It does this by storing the ID from two separate tables. This creates a link. Our 'Category' table will always be a 'Category' so we include 'category_id.' The tables it links to vary, so we add an item 'taggable_id' which holds the id of any taggable item. Then, we use 'taggable_type' to complete the link allowing the link to know what it is linked to, such as an article.
Now, we need to set up our models:
class Article < ActiveRecord::Base has_many :tags, :as => :taggable, :dependent => :destroy has_many :categories, :through => :tagsendclass Category < ActiveRecord::Base has_many :tags, :dependent => :destroy has_many :articles, :through => :tags, :source => :taggable, :source_type => 'Article'endclass Tag < ActiveRecord::Base belongs_to :taggable, :polymorphic => true belongs_to :categoryend
After this, setup your database using:
rake db:migrate
That's it! Now, you can setup your database with real data:
Category.create :name => "Food"Article.create :name => "Picking the right restaurant."Article.create :name => "The perfect cherry pie!"Article.create :name => "Foods to avoid when in a hurry!"Category.create :name => "Kitchen"Article.create :name => "The buyers guide to great refrigeration units."Article.create :name => "The best stove for your money."Category.create :name => "Beverages"Article.create :name => "How to: Make your own soda."Article.create :name => "How to: Fermenting fruit."
Now you have a few categories and various articles. They are not categorized using tags, however. So, we will need to do that:
a = Tag.newa.taggable = Article.find_by_name("Picking the right restaurant.")a.category = Category.find_by_name("Food")a.save
You could then repeat this for each, this will link your categories and articles. After doing this you will be able to access each article's categories and each categorie's articles:
Article.first.categoriesCategory.first.articles
Notes:
1)Whenever you want to delete an item that is linked by a link-model make sure to use "destroy." When you destroy a linked object, it will also destroy the link. This ensures that there are no bad or dead links. This is why we use ':dependent => :destroy'
2)When setting up our 'Article' model, which is one our 'taggable' models, it must be linked using :as. Since in the preceeding example we used 'taggable_type' and 'taggable_id' we use :as => :taggable. This helps rails know how to store the values in the database.
3)When linking categories to articles, we use: has_many :articles, :through => :tags, :source => :taggable, :source_type => 'Article' This tells the category model that it should have many :articles through :tags. The source is :taggable, for the same reason as above. The source-type is "Article" because a model will automatically set taggable_type to its own name.
- has_many polymorphic
- rails has_many on polymorphic
- has_many through(多对多) 与polymorphic(多态)混合使用
- has_many 和belongs_to
- Polymorphic Viruses
- Polymorphic Protector
- 多态(polymorphic)
- polymorphic read
- polymorphic--dynaminc binding
- Polymorphic DLL UID summary
- Polymorphic DLL UID summary
- Java Programming : Polymorphic Arguments
- Java Note (4) Polymorphic
- 1000. About Polymorphic
- Polymorphic Inline Caches explained
- index and polymorphic
- Example Of has_many In Rails's ActiveRecord
- has_many,class_name,foreign_key,include?,through,source
- IOS_自定义UIButton的子视图位置 EdgeInsets属性
- 数据库出现"可疑"
- 几种经典的网络服务器架构模型的分析与比较
- Jquery实现无刷新验证码
- 一篇时常翻出来看的所收藏文章之一测试管理经验的心得
- has_many polymorphic
- Android进阶:保持android手机屏幕灯持续高亮 以及键盘灯高亮
- Android与webservice进行实体对象的传递
- 控件RadioGroup、RadioButton和CheckBox的使用
- Android内核源码下载与编译
- jquery 容易被忽略的几个方法,会了你就是高手了
- android工程目录结构
- PRD产品需求文档
- 2> /dev/null的解释