Rails has_one :通过自引用 1-to-1-to-1 协会
我有一个 Factsheet
模型,其中包含各种出版物,包括替代语言版本。不同的语言版本应作为单独的记录分开保存(因为它们可以单独订购/更新/等),但我试图将它们彼此关联,以便您可以轻松判断一份出版物何时是西班牙语(或中文等)版本等。
我想使用 :through
关联,以便关系是对称的,例如,如果英语资料表 A 有西班牙语版本资料表 B,那么类似地资料表 B 有英语版本资料表 A。
这是我的模型:
class Factsheet < ActiveRecord::Base
has_many :publications_language_relationships
has_one :en, :through => :publications_language_relationships
has_one :es, :through => :publications_language_relationships
has_one :zh, :through => :publications_language_relationships #zh = Chinese
# other stuff
end
并且...
# Table name: publications_language_relationships
#
# en_id :integer
# es_id :integer
# zh_id :integer
#
class PublicationsLanguageRelationship < ActiveRecord::Base
belongs_to :en, :class_name => 'Factsheet'
belongs_to :es, :class_name => 'Factsheet'
belongs_to :zh, :class_name => 'Factsheet'
end
但是当我启动 Rails 控制台来检查它是否完全有效时...
$ fs = Factsheet.last
=> #<Factsheet id: 5, title: "Despu\xC3\xA9s de un diagn\xC3\xB3stico de c\xC3\
xA1ncer de seno: Con...", backend_code: "fs_after_bc_diagnosis_es", language:
"es", created_at: "2010-11-30 21:23:01", updated_at: "2010-12-06 16:13:23">
$ fs.en
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: publicati
ons_language_relationships.factsheet_id: SELECT "factsheets".* FROM "factsheets"
INNER JOIN "publications_language_relationships" ON "factsheets".id = "publicat
ions_language_relationships".en_id WHERE (("publications_language_relationships"
.factsheet_id = 5)) LIMIT 1
所以我的关联出了问题,但我不太确定是什么。想法?
此外,这对于数据来说是否是一个合理的设计,或者我应该在这里做一些不同的事情?
I have a Factsheet
model which holds an assortment of publications, including alternate language versions. The different language versions should be kept separate as individual records (because they can be ordered/updated/etc. separately), but I'm trying to associate them to each other so that you can easily tell when one publication is the Spanish (or Chinese, etc.) version of the other.
I would like to use a :through
association so that the relationship is symmetric, e.g. if English Factsheet A has a Spanish version Factsheet B, then similarly Factsheet B has an English version Factsheet A.
Here are my models:
class Factsheet < ActiveRecord::Base
has_many :publications_language_relationships
has_one :en, :through => :publications_language_relationships
has_one :es, :through => :publications_language_relationships
has_one :zh, :through => :publications_language_relationships #zh = Chinese
# other stuff
end
and...
# Table name: publications_language_relationships
#
# en_id :integer
# es_id :integer
# zh_id :integer
#
class PublicationsLanguageRelationship < ActiveRecord::Base
belongs_to :en, :class_name => 'Factsheet'
belongs_to :es, :class_name => 'Factsheet'
belongs_to :zh, :class_name => 'Factsheet'
end
But when I fire up a Rails console to check to see if that works at all...
$ fs = Factsheet.last
=> #<Factsheet id: 5, title: "Despu\xC3\xA9s de un diagn\xC3\xB3stico de c\xC3\
xA1ncer de seno: Con...", backend_code: "fs_after_bc_diagnosis_es", language:
"es", created_at: "2010-11-30 21:23:01", updated_at: "2010-12-06 16:13:23">
$ fs.en
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: publicati
ons_language_relationships.factsheet_id: SELECT "factsheets".* FROM "factsheets"
INNER JOIN "publications_language_relationships" ON "factsheets".id = "publicat
ions_language_relationships".en_id WHERE (("publications_language_relationships"
.factsheet_id = 5)) LIMIT 1
So something's amiss with my associations, but I'm not quite sure what. Thoughts?
Additionally, is this even a sound design for the data, or should I be doing something differently here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我还没有测试这个解决方案,但我认为您想要采取的一般架构方向并不是真正通过 :through 关系,而是类似:
然后,我认为您会在控制器/视图中调用翻译像这样:
我认为这不是绝对正确的,但无论如何它应该让你走上这条路。
I haven't tested this solution, but I think the general architectural direction you want to take is not really through the :through relationship, but rather something like:
And then, I think you'd call the translations in your controller/views something like this:
I don't think this is absolutely right, but it should get you on the path anyway.