那么这是我的问题我有三张桌子; 地区,国家,国家.国家可以在区域内,州可以在区域内.地区是食物链的顶端.
现在我添加一个包含两列的popular_areas表; region_id和popular_place_id.是否有可能使popular_place_id成为国家或州的外键.我可能不得不添加一个popular_place_type列来确定id是否描述了一个国家或州.
我有一个Products表,想要添加一列:
t.references :imageable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)
我试图通过以下方式为此生成迁移:
$ rails generate migration AddImageableToProducts imageable:references:polymorphic
Run Code Online (Sandbox Code Playgroud)
但我显然做错了.任何人都可以提出任何建议吗?谢谢
当我在生成迁移后尝试手动将其放入时,我这样做:
class AddImageableToProducts < ActiveRecord::Migration
def self.up
add_column :products, :imageable, :references, :polymorphic => true
end
def self.down
remove_column :products, :imageable
end
end
Run Code Online (Sandbox Code Playgroud)
它仍然没有奏效
伙计们,
想确保我理解正确.请忽略继承的情况(SentientBeing),尝试着重于has_many中的多态模型:通过关系.也就是说,考虑以下......
class Widget < ActiveRecord::Base
has_many :widget_groupings
has_many :people, :through => :widget_groupings, :source => :person, :conditions => "widget_groupings.grouper_type = 'Person'"
has_many :aliens, :through => :widget_groupings, :source => :alien, :conditions => "video_groupings.grouper_type = 'Alien'"
end
class Person < ActiveRecord::Base
has_many :widget_groupings, :as => grouper
has_many :widgets, :through => :widget_groupings
end
class Alien < ActiveRecord::Base
has_many :widget_groupings, :as => grouper
has_many :widgets, :through => :widget_groupings
end
class WidgetGrouping < ActiveRecord::Base
belongs_to :widget
belongs_to :grouper, :polymorphic => true
end
Run Code Online (Sandbox Code Playgroud)
在一个完美的世界里,我想,给一个Widget和一个人,做一些像:
widget.people << …Run Code Online (Sandbox Code Playgroud) 为什么在多态关联中没有外键,例如下面表示为Rails模型的外键?
class Comment < ActiveRecord::Base
belongs_to :commentable, :polymorphic => true
end
class Article < ActiveRecord::Base
has_many :comments, :as => :commentable
end
class Photo < ActiveRecord::Base
has_many :comments, :as => :commentable
#...
end
class Event < ActiveRecord::Base
has_many :comments, :as => :commentable
end
Run Code Online (Sandbox Code Playgroud) database ruby-on-rails foreign-key-relationship polymorphic-associations
我的应用程序中有以下"评论"表:
comments
--------
id INT
foreign_id INT
model TEXT
comment_text TEXT
...
Run Code Online (Sandbox Code Playgroud)
这个表的想法是存储我的应用程序的各个部分的注释 - 它可以存储博客帖子的评论,即:
1|34|blogpost|lorem ipsum...
Run Code Online (Sandbox Code Playgroud)
用户图片:
2|12|picture|lorem ipsum...
Run Code Online (Sandbox Code Playgroud)
等等.
现在,有没有办法强制FOREIGN KEY约束这些数据?
即在评论表中这样的东西:
FOREIGN KEY (`foreign_id`) REFERENCES blogposts (`id`)
//but only when model='blogpost'
Run Code Online (Sandbox Code Playgroud) mysql sql database-design foreign-keys polymorphic-associations
我的问题与此问题基本相同: 在同一模型上具有多个关联的多态关联
但是,提议/接受的解决方案不起作用,如后面的评论者所示.
我有一个Photo类,在我的应用程序中使用.帖子可以有一张照片.但是,我想重新使用多态关系来添加辅助照片.
之前:
class Photo
belongs_to :attachable, :polymorphic => true
end
class Post
has_one :photo, :as => :attachable, :dependent => :destroy
end
Run Code Online (Sandbox Code Playgroud)
期望:
class Photo
belongs_to :attachable, :polymorphic => true
end
class Post
has_one :photo, :as => :attachable, :dependent => :destroy
has_one :secondary_photo, :as => :attachable, :dependent => :destroy
end
Run Code Online (Sandbox Code Playgroud)
但是,由于无法找到"SecondaryPhoto"类,因此失败.基于我从其他线程可以看出的内容,我想做:
has_one :secondary_photo, :as => :attachable, :class_name => "Photo", :dependent => :destroy
Run Code Online (Sandbox Code Playgroud)
除了调用Post#secondary_photo之外,只返回通过Photo Association附加的相同照片,例如Post#photo === Post#secondary_photo.看看SQL,它确实WHERE type ="Photo"而不是像我想要的那样"SecondaryPhoto"......
思考?谢谢!
我想建立一个多态关系accepts_nested_attributes_for.这是代码:
class Contact <ActiveRecord::Base
has_many :jobs, :as=>:client
end
class Job <ActiveRecord::Base
belongs_to :client, :polymorphic=>:true
accepts_nested_attributes_for :client
end
Run Code Online (Sandbox Code Playgroud)
当我试图访问Job.create(..., :client_attributes=>{...}给我NameError: uninitialized constant Job::Client
ruby ruby-on-rails polymorphic-associations nested-attributes
我有很多实例需要在我的数据库中实现某种多态关联.我总是浪费大量的时间来思考所有的选择.这是我能想到的3.我希望有一个SQL Server的最佳实践.
这是多列方法

这是没有外键的方法

这是基表方法

sql-server associations polymorphic-associations database-normalization
我有一个多态关联和STI的案例.
# app/models/car.rb
class Car < ActiveRecord::Base
belongs_to :borrowable, :polymorphic => true
end
# app/models/staff.rb
class Staff < ActiveRecord::Base
has_one :car, :as => :borrowable, :dependent => :destroy
end
# app/models/guard.rb
class Guard < Staff
end
Run Code Online (Sandbox Code Playgroud)
为了使多态关联起作用,根据多态关联的API文档,http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Polymorphic+Associations 我必须设置borrowable_type为base_classSTI模型,在我的情况下是Staff.
问题是:如果borrowable_type设置为STI类,为什么它不起作用?
一些测试来证明它:
# now the test speaks only truth
# test/fixtures/cars.yml
one:
name: Enzo
borrowable: staff (Staff)
two:
name: Mustang
borrowable: guard (Guard)
# test/fixtures/staffs.yml
staff:
name: Jullia Gillard
guard:
name: Joni Bravo …Run Code Online (Sandbox Code Playgroud) A Person可以有很多Events,每个都Event可以有一个多态Eventable记录.如何指定记录Person与Eventable记录之间的关系?
以下是我的模型:
class Event < ActiveRecord::Base
belongs_to :person
belongs_to :eventable, :polymorphic => true
end
class Meal < ActiveRecord::Base
has_one :event, :as => eventable
end
class Workout < ActiveRecord::Base
has_one :event, :as => eventable
end
Run Code Online (Sandbox Code Playgroud)
主要问题涉及到Person班级:
class Person < ActiveRecord::Base
has_many :events
has_many :eventables, :through => :events # is this correct???
end
Run Code Online (Sandbox Code Playgroud)
我是否has_many :eventables, :through => :events像上面那样说?
或者我必须像这样拼写它们:
has_many :meals, :through => :events
has_many :workouts, …Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails has-many-through polymorphic-associations
activerecord ×3
mysql ×2
associations ×1
database ×1
foreign-keys ×1
ruby ×1
sql ×1
sql-server ×1