如何在具有单独外键的 rails 模型中使用 through
how to use through in rails model with separate foreign keys
我有一个 rails 应用程序,其中包含以下型号。我希望能够直接从任务中引用配置文件。正如我所见,我应该以某种方式使用 :through ,但我无法弄清楚。目前假设如果我想在任务模型中获得 first_name,我必须使用 task.executor.profile.first_name。相反,我想使用 task.executor_profile.first_name。
我需要它进行搜查,您可以在其中参考与 include 的关联。如果有更简单的解决方案而无需执行 :through 关联,请告诉我。
更新:
基于@rlarcombe,我尝试了委托,但不幸的是,Ransack 似乎不支持该解决方案,但它与 rails 配合得很好。 sby 能告诉我在这种情况下如何使用 :through 关联吗?
user.rb
has_many :assigned_tasks, class_name: "Task", foreign_key: "assigner_id", dependent: :destroy
has_many :executed_tasks, class_name: "Task", foreign_key: "executor_id", dependent: :destroy
has_one :profile, dependent: :destroy
profile.rb
belongs_to :user
task.rb
belongs_to :assigner, class_name: "User"
belongs_to :executor, class_name: "User"
您所要做的就是在您的任务模型上定义几个 has_one through associations。
这些关联定义应该可以满足您的需求:
app/models/task.rb
class Task < ActiveRecord::Base
belongs_to :assigner, class_name: "User"
belongs_to :executor, class_name: "User"
has_one :assigner_profile, through: :assigner, source: :profile
has_one :executor_profile, through: :executor, source: :profile
end
app/models/user.rb
class User < ActiveRecord::Base
has_many :assigned_tasks, class_name: "Task", foreign_key: "assigner_id", dependent: :destroy
has_many :executed_tasks, class_name: "Task", foreign_key: "executor_id", dependent: :destroy
has_one :profile, dependent: :destroy
end
app/models/profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
end
有了这些,您现在应该可以调用:
task.assigner_profile.first_name
和
task.executor_profile.first_name
这些 has_one 通过关联应该与 Ransack 一起正常工作。
谢谢
我有一个 rails 应用程序,其中包含以下型号。我希望能够直接从任务中引用配置文件。正如我所见,我应该以某种方式使用 :through ,但我无法弄清楚。目前假设如果我想在任务模型中获得 first_name,我必须使用 task.executor.profile.first_name。相反,我想使用 task.executor_profile.first_name。
我需要它进行搜查,您可以在其中参考与 include 的关联。如果有更简单的解决方案而无需执行 :through 关联,请告诉我。
更新:
基于@rlarcombe,我尝试了委托,但不幸的是,Ransack 似乎不支持该解决方案,但它与 rails 配合得很好。 sby 能告诉我在这种情况下如何使用 :through 关联吗?
user.rb
has_many :assigned_tasks, class_name: "Task", foreign_key: "assigner_id", dependent: :destroy
has_many :executed_tasks, class_name: "Task", foreign_key: "executor_id", dependent: :destroy
has_one :profile, dependent: :destroy
profile.rb
belongs_to :user
task.rb
belongs_to :assigner, class_name: "User"
belongs_to :executor, class_name: "User"
您所要做的就是在您的任务模型上定义几个 has_one through associations。
这些关联定义应该可以满足您的需求:
app/models/task.rb
class Task < ActiveRecord::Base
belongs_to :assigner, class_name: "User"
belongs_to :executor, class_name: "User"
has_one :assigner_profile, through: :assigner, source: :profile
has_one :executor_profile, through: :executor, source: :profile
end
app/models/user.rb
class User < ActiveRecord::Base
has_many :assigned_tasks, class_name: "Task", foreign_key: "assigner_id", dependent: :destroy
has_many :executed_tasks, class_name: "Task", foreign_key: "executor_id", dependent: :destroy
has_one :profile, dependent: :destroy
end
app/models/profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
end
有了这些,您现在应该可以调用:
task.assigner_profile.first_name
和
task.executor_profile.first_name
这些 has_one 通过关联应该与 Ransack 一起正常工作。
谢谢