英文:
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
我需要以某种方式将它们连接起来,以便 story
的 authorId
与 user
的 uid
的匹配值相连。
我已经查看了类似于 这个 和 这个 的问题,以及 文档,但我找不到有效的模型代码。
目前,对于 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 "stories", force: :cascade do |t|
t.string "title"
t.integer "authorId"
...
end
And here's the structure for the User
table:
create_table "users", force: :cascade do |t|
...
t.integer "uid"
...
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 < ApplicationRecord
belongs_to :user, class_name: 'User', foreign_key: 'authorId'
end
And for user.rb
(I'm using Devise.):
class User < 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: "Story", foreign_key: 'uid'
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 < 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论