Rails: 使用非id外键/主键的belongs_to/has_many

huangapple go评论58阅读模式
英文:

Rails: belongs_to/has_many with non-id foreign/primary keys

问题

以下是翻译好的部分:

我正在处理一个遗留数据库,其中一个 story 属于一个 user,而一个 user 拥有多个 stories。问题在于这是上传的数据,所以它们不能像 Rails 通常做的那样连接到 id 字段。

这是 Story 表的结构:

create_table "stories", force: :cascade do |t|
  t.string "title"
  t.integer "authorId"
  ...
end

这是 User 表的结构:

create_table "users", force: :cascade do |t|
  ...
  t.integer "uid"
  ...
end

我需要以某种方式将它们连接起来,以便 storyauthorIduseruid 的匹配值相连。

我已经查看了类似于 这个这个 的问题,以及 文档,但我找不到有效的模型代码。

目前,对于 story.rb,我有以下代码:

class Story < ApplicationRecord
  belongs_to :user, class_name: 'User', foreign_key: 'authorId'
end

对于 user.rb(我正在使用 Devise):

class User < ApplicationRecord
  # 包括默认的 Devise 模块。其他可用的模块包括:
  # :confirmable, :lockable, :timeoutable 和 :omniauthable
  devise :database_authenticatable, :registerable, :trackable,
         :recoverable, :rememberable, :validatable

  has_many :stories, class_name: "Story", foreign_key: 'uid'
end

有人能看出如何在 belongs_to/has_many 中正确连接这些模型吗?

英文:

I'm working with a legacy database where a story belongs_to a user and a user has_many stories. The problem is that it's all uploaded data, so they can't connect with the id field like Rails usually does.

Here's the structure for the Story table:

  create_table &quot;stories&quot;, force: :cascade do |t|
    t.string &quot;title&quot;
    t.integer &quot;authorId&quot;
    ...
  end

And here's the structure for the User table:

create_table &quot;users&quot;, force: :cascade do |t|
    ...
    t.integer &quot;uid&quot;
    ...
end

I need to somehow join them so that the story's authorId connects to matching values for a user's uid.

I've looked at questions like this and this, as well as the documentation, but I can't find model code that works.

Right now what I have is, for story.rb:

class Story &lt; ApplicationRecord
	belongs_to :user, class_name: &#39;User&#39;, foreign_key: &#39;authorId&#39;
end

And for user.rb (I'm using Devise.):

class User &lt; ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, and :omniauthable
  devise :database_authenticatable, :registerable, :trackable,
         :recoverable, :rememberable, :validatable

  has_many :stories, class_name: &quot;Story&quot;, foreign_key: &#39;uid&#39;
end

Can anyone see how to get these models properly linked up in a belongs_to/has_many?

答案1

得分: 1

I believe by your post that you cannot update the AuthorID to user_id, and change uid to id. After that, you would have a clean relationship.

But, try to define the primary_key and foreign_key and customize your association:

class Story < ApplicationRecord
  belongs_to :user, primary_key: "uid", foreign_key: "authorId"
end

class User < ApplicationRecord
  has_many :stories, primary_key: "authorId"
end

or this:

class Story < ApplicationRecord
  belongs_to :user, primary_key: "uid"
end

class User < ApplicationRecord
  has_many :stories, primary_key: "authorId", foreign_key: "uid"
end

I think this maybe works.

英文:

I believe by your post that you cannot update the AuthorID to user_id, and change uid to id. After that, you would have a clean relationship.

But, try to define the primary_key and foreign_key and customize your association:

class Story &lt; ApplicationRecord
 belongs_to :user, primary_key: &quot;uid&quot;, foreign_key: &quot;authorId&quot;
end

class User &lt; ApplicationRecord
  has_many :stories, primary_key: &quot;authorId&quot;
end

or this:

class Story &lt; ApplicationRecord
 belongs_to :user, primary_key: &quot;uid&quot;
end

class User &lt; ApplicationRecord
  has_many :stories, primary_key: &quot;authorId&quot;, foreign_key: &quot;uid&quot;
end

I think this maybe works.

huangapple
  • 本文由 发表于 2023年6月15日 06:27:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定