Rails:非 id 外键查找 ActiveRecord

发布于 2024-09-11 06:40:48 字数 742 浏览 5 评论 0原文

我希望 ActiveRecord 通过表中的非 id 列进行查找。 希望当我向您提供代码示例时您能清楚地了解这一点。

class CoachClass < ActiveRecord::Base
  belongs_to :coach
end

class Coach < ActiveRecord::Base
    has_many :coach_classes, :foreign_key => 'user_name'
end

当我做一个 coach_obj.coach_classes,这正确地触发了

SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 2)

(2是那个教练的id,这是我的问题。)

我希望它触发

SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 'David')

(“David”是那个教练的>user_name)

user_name 是唯一的并且存在于两个表中。

由于某种原因,我不想在我的 coach_classes 表中包含 coach_id

I want ActiveRecord to lookup by a non-id column from a table.
Hope this is clear when I give you my code sample.

class CoachClass < ActiveRecord::Base
  belongs_to :coach
end

class Coach < ActiveRecord::Base
    has_many :coach_classes, :foreign_key => 'user_name'
end

When I do a
coach_obj.coach_classes, this rightly triggers

SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 2)

(2 being the that coach's id here which is my problem.)

I want it to trigger

SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = 'David')

('David' being the that coach's user_name)

user_name is unique and present in both tables.

I do not want to have a coach_id in my coach_classes table for some reason.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

始终不够爱げ你 2024-09-18 06:40:48

我认为您还需要指定关联的主键选项:

class CoachClass < ActiveRecord::Base 
  belongs_to :coach, :foreign_key => 'user_name', :primary_key => 'user_name'
end

class Coach < ActiveRecord::Base
  has_many :coach_classes, :foreign_key => 'user_name', :primary_key => 'user_name'
end 

这指定返回关联对象的主键的方法(默认为id)。

I think you need to specify the primary key options on the associations as well:

class CoachClass < ActiveRecord::Base 
  belongs_to :coach, :foreign_key => 'user_name', :primary_key => 'user_name'
end

class Coach < ActiveRecord::Base
  has_many :coach_classes, :foreign_key => 'user_name', :primary_key => 'user_name'
end 

This specifies the method that returns the primary key of the associated object (defaulting to id).

江挽川 2024-09-18 06:40:48

有一个名为 primary_key 的选项,默认设置为 :id。您想要使用:

has_many :coach_classes, :foreign_key => :user_name, :primary_key => :user_name

同时在 belongs_to 关联上使用这些选项。

请参阅文档了解更多信息。

There is a option called primary_key which is per default set to :id. You want to use:

has_many :coach_classes, :foreign_key => :user_name, :primary_key => :user_name

Also use these options on the belongs_to association.

Read more in the documentation.

一萌ing 2024-09-18 06:40:48

您需要使用finder_sql

class Coach < ActiveRecord::Base
    has_many :coach_classes, :finder_sql => 'SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = "#{user_name}")'
end

You need to use finder_sql:

class Coach < ActiveRecord::Base
    has_many :coach_classes, :finder_sql => 'SELECT * FROM `coach_classes` WHERE (`coach_classes`.user_name = "#{user_name}")'
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文