Rails has_one :通过自引用 1-to-1-to-1 协会

发布于 2024-10-06 08:02:23 字数 1748 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脱离于你 2024-10-13 08:02:23

我还没有测试这个解决方案,但我认为您想要采取的一般架构方向并不是真正通过 :through 关系,而是类似:

class Factsheet < ActiveRecord::Base
  has_many :publications_language_relationships

   named_scope :translation, lambda { |trans|
      { :conditions => ["publications_language_relationships = ?", trans.to_s ] , 
        :joins => :publications_language_relationships 
      }
    }

  # other stuff
end

class PublicationsLanguageRelationship < ActiveRecord::Base
  belongs_to :fact_sheet
end

然后,我认为您会在控制器/视图中调用翻译像这样:

#controller
def show
   @fact_sheet = FactSheet.find( params[:id] ) # to load up the FactSheet
end

#view (to get the right translation)
@fact_sheet.translation(:en) #for english

我认为这不是绝对正确的,但无论如何它应该让你走上这条路。

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:

class Factsheet < ActiveRecord::Base
  has_many :publications_language_relationships

   named_scope :translation, lambda { |trans|
      { :conditions => ["publications_language_relationships = ?", trans.to_s ] , 
        :joins => :publications_language_relationships 
      }
    }

  # other stuff
end

class PublicationsLanguageRelationship < ActiveRecord::Base
  belongs_to :fact_sheet
end

And then, I think you'd call the translations in your controller/views something like this:

#controller
def show
   @fact_sheet = FactSheet.find( params[:id] ) # to load up the FactSheet
end

#view (to get the right translation)
@fact_sheet.translation(:en) #for english

I don't think this is absolutely right, but it should get you on the path anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文