Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 建立多态关系有很多:通过关系_Ruby On Rails 3_Polymorphism - Fatal编程技术网

Ruby on rails 3 建立多态关系有很多:通过关系

Ruby on rails 3 建立多态关系有很多:通过关系,ruby-on-rails-3,polymorphism,Ruby On Rails 3,Polymorphism,我已经创建了前面代码中所示的模型。文章将是许多可以有标签的模型之一。类别模型将包含可能分配的所有类别。标记模型将是表示标记关系的多态联接表 rails g model Article name:string rails g model Category name:string rails g model Tag name:string taggable_id:integer taggable_type:string category_id:integer 类文章:taggable 有多个:类别

我已经创建了前面代码中所示的模型。文章将是许多可以有标签的模型之一。类别模型将包含可能分配的所有类别。标记模型将是表示标记关系的多态联接表

rails g model Article name:string
rails g model Category name:string
rails g model Tag name:string taggable_id:integer taggable_type:string category_id:integer
类文章:taggable
有多个:类别,:至=>:标记
结束
类类别:taggable
有很多:文章,:至=>:taggable
结束
类标记true
属于:类别
结束
我似乎不能让这个工作,我可以做它的非多态性,但我必须有一些错误的多态性部分。有什么想法吗

编辑:仍然无法正确执行此操作:

class Article < ActiveRecord::Base
  has_many :tags, :as => :taggable
  has_many :categories, :through => :taggable
end

class Category < ActiveRecord::Base
  has_many :tags, :as => :taggable
  has_many :articles, :through => :taggable
end

class Tag < ActiveRecord::Base
  belongs_to :taggable, :polymorphic => true
  belongs_to :category
end
类文章:tag
有很多:类别,:通过=>:taggables,:source=>:tag,:source\u type=>“文章”
结束
类类别:tag
有很多:文章,:至=>:taggables,:source=>:tag,:source\u type=>“文章”
结束
类标记true
属于:类别
结束

您根本无法使联接表多态,至少Rails不支持这种开箱即用的方式。解决方案如下(摘自Obie的Rails 3-way):

如果您确实需要它,
有很多:通过多态关联可以实现,但只需指定您想要的多态关联类型即可。为此,必须使用
:source\u type
选项。在大多数情况下,您必须使用
:source
选项,因为关联名称与多态关联使用的接口名称不匹配:


我希望我能帮忙

要创建一个多态模型,有很多:到,您必须首先创建您的模型。我们将使用“Article”、“Category”和“Tag”,其中“Tag”是连接模型,而Article是可以使用类别“标记”的许多对象之一

首先,创建“文章”和“类别”模型。这些是基本模型,目前还不需要特别注意:

User.first.commented_timesheets
现在,我们将创建多态联接表:

rails g model Article name:string
rails g model Category name:string
联接表将两个表联接在一起,或者在本例中,通过多态行为将一个表联接到多个其他表。它通过存储来自两个单独表的ID来实现这一点。这将创建一个链接。我们的“Category”表始终是一个“Category”,因此我们包含“Category\u id”。它链接到的表各不相同,因此我们添加了一个项“taggable\u id”,它保存任何taggable项的id。然后,我们使用“taggable_type”完成链接,让链接知道它链接到什么,比如一篇文章

现在,我们需要建立我们的模型:

rails g model Tag taggable_id:integer taggable_type:string category_id:integer
就这样!现在,您可以使用真实数据设置数据库:

rake db:migrate
现在您有了一些类别和各种文章。但是,它们没有使用标记进行分类。因此,我们需要这样做:

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."
然后你可以对每个人重复这个,这将链接你的类别和文章。完成此操作后,您将能够访问每篇文章的类别和每个类别的文章:

a = Tag.new
a.taggable = Article.find_by_name("Picking the right restaurant.")
a.category = Category.find_by_name("Food")
a.save
注:

1) 每当您要删除链接模型链接的项目时,请确保使用“销毁”。当您销毁链接对象时,它也会销毁链接。这样可以确保没有坏链接或死链接。这就是为什么我们使用“:dependent=>:destroy”

2) 当设置我们的“文章”模型时,这是我们的“标记”模型之一,它必须使用:as链接。因为在前面的示例中,我们使用了“taggable_type”和“taggable_id”,所以我们使用:as=>:taggable。这有助于rails了解如何在数据库中存储值

3) 将类别链接到文章时,我们使用: 有很多:文章,:至=>:tags,:source=>:taggable,:source\u type=>'Article'
这告诉类别模型它应该有很多:article到:标记。源代码为:taggable,原因与上述相同。源类型为“Article”,因为模型会自动将taggable_类型设置为自己的名称。

今天我将尝试一下,看看我是否完全理解如何做到这一点。你能在迁移中包括什么样的最佳索引吗?视图如何工作?我可以让它渲染,但不能保存到数据库。我试过:
“最多选择3个”},{:class=>“o-input--quiet”}%>
rake db:migrate
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."
a = Tag.new
a.taggable = Article.find_by_name("Picking the right restaurant.")
a.category = Category.find_by_name("Food")
a.save
Article.first.categories
Category.first.articles