我正在阅读有关自我参照has_many:通过今天的数据情况,因为我正在尝试构建一个使用它们的Rails应用程序.我在互联网上发现了这个例子,我对此有疑问.让我在这个人的博客上发布这个示例代码:
create_table :animals do |t|
t.string :species
end
create_table :hunts do |t|
t.integer :predator_id
t.integer :prey_id
t.integer :capture_percent
end
class Animal < ActiveRecord::Base
has_many :pursuits, :foreign_key => 'predator_id',
:class_name => 'Hunt',
:dependent => :destroy
has_many :preys, :through => :pursuits
has_many :escapes, :foreign_key => 'prey_id',
:class_name => 'Hunt',
:dependent => :destroy
has_many :predators, :through => :escapes
end
class Hunt < ActiveRecord::Base
belongs_to :predator, :class_name => "Animal"
belongs_to :prey, :class_name => "Animal"
end
Run Code Online (Sandbox Code Playgroud)
假设我正在构建一个列出其数据库中第一个动物的网页.此标题下方是百分比列表(capture_percent).每个百分比指的是这个页面的动物狩猎的动物,但它不会告诉你动物的名字,只是百分比.单击任何给定的百分比将转到相应的动物页面.
我知道,这是一个难以解决的问题,但我正在努力解决这个问题.我是否必须为CapturePercent创建一个单独的表?
我正在使用<<将对象添加到集合中
current_user.followers<<users
Run Code Online (Sandbox Code Playgroud)
当用户中的用户已存在于关注者集合中时,我想捕获异常.我怎样才能做到这一点?
谢谢.
PS.通过输入"<<"在Google上找到结果真的很难,是否有名称来调用此方法?