【问题标题】:Rails has_many through helper methods using non-primary keysRails has_many 通过辅助方法使用非主键
【发布时间】:2016-02-06 16:37:40
【问题描述】:

http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many

一个Firm 类声明has_many :clients,将添加:

Firm#client_ids
Firm#client_ids=

有没有办法更改关联,以便使用客户端表中的非主键生成以下帮助程序。

Firm#client_uuids
Firm#client_uuids=

我的连接表使用 UUID,并且 has_many through 关系声明了正确的主键和外键。但是这些辅助方法不会更新。

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-4 activerecord has-many-through


【解决方案1】:

不。那里的命名是因为它是客户的.id。如果你真的关心这个命名,你可以给方法起别名。

恕我直言,使用名称属性_uuid 通常很糟糕;它是 UUID/GUID/Integer/等的事实。不应该体现在命名+是一个实现细节。我仍然在使用我已经完成的架构,然后需要更改主键策略。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 2016-07-22
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多