主动关系:通过关联检索记录?

发布于 2024-12-03 10:22:55 字数 1211 浏览 6 评论 0原文

我有以下模型:

class User < ActiveRecord::Base
  has_many :survey_takings
end

class SurveyTaking < ActiveRecord::Base
  belongs_to :survey

  def self.surveys_taken # must return surveys, not survey_takings
    where(:state => 'completed').map(&:survey)
  end

  def self.last_survey_taken
    surveys_taken.maximum(:position) # that's Survey#position
  end
end

目标是能够从控制器调用 @user.survey_taken.last_survey_taken。 (这是人为的,但就这样吧;总体目标是能够调用 @user.survey_takes 上的类方法,这些方法可以使用关联调查的关系。)

在当前的形式中,此代码赢得了胜利不工作;当我调用 .map(&:survey) 时,surveys_taken 将 ActiveRelation 折叠到数组中。有没有某种方法可以返回所有加入调查的关系?我不能这样做:

def self.surveys_taken
  Survey.join(:survey_takings).where("survey_takings.state = 'completed'")
end

因为@user.survey_taken.surveys_taken会加入所有已完成的survey_taken,而不仅仅是@user。

我想我想要的是等价的

class User < ActiveRecord::Base
  has_many :survey_takings
  has_many :surveys_taken, :through => :survey_takings, :source => :surveys
end

,但我无法从 SurveyTaking.last_survey_taken 访问该 Surveys_taken 关联。

I have the following models:

class User < ActiveRecord::Base
  has_many :survey_takings
end

class SurveyTaking < ActiveRecord::Base
  belongs_to :survey

  def self.surveys_taken # must return surveys, not survey_takings
    where(:state => 'completed').map(&:survey)
  end

  def self.last_survey_taken
    surveys_taken.maximum(:position) # that's Survey#position
  end
end

The goal is to be able to call @user.survey_takings.last_survey_taken from a controller. (That's contrived, but go with it; the general goal is to be able to call class methods on @user.survey_takings that can use relations on the associated surveys.)

In its current form, this code won't work; surveys_taken collapses the ActiveRelation into an array when I call .map(&:survey). Is there some way to instead return a relation for all the joined surveys? I can't just do this:

def self.surveys_taken
  Survey.join(:survey_takings).where("survey_takings.state = 'completed'")
end

because @user.survey_takings.surveys_taken would join all the completed survey_takings, not just the completed survey_takings for @user.

I guess what I want is the equivalent of

class User < ActiveRecord::Base
  has_many :survey_takings
  has_many :surveys_taken, :through => :survey_takings, :source => :surveys
end

but I can't access that surveys_taken association from SurveyTaking.last_survey_taken.

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

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

发布评论

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

评论(2

乱了心跳 2024-12-10 10:22:55

如果我理解正确的话,您想查找某个用户完成的调查吗?如果是这样,你可以这样做:

Survey.join(:survey_takings).where("survey_takings.state = 'completed'", :user => @user)

而且它看起来像而不是:

def self.surveys_taken
where(:state => 'completed').map(&:survey)
end

你可能想使用范围:

scope :surveys_taken, where(:state => 'completed')

If I'm understanding correctly you want to find completed surveys by a certain user? If so you can do:

Survey.join(:survey_takings).where("survey_takings.state = 'completed'", :user => @user)

Also it looks like instead of:

def self.surveys_taken
where(:state => 'completed').map(&:survey)
end

You may want to use scopes:

scope :surveys_taken, where(:state => 'completed')
云裳 2024-12-10 10:22:55

我想我正在寻找的是这样的:

class SurveyTaking < ActiveRecord::Base
  def self.surveys_taken
    Survey.joins(:survey_takings).where("survey_takings.state = 'completed'").merge(self.scoped)
  end
end

这样, SurveyTaking.surveys_taken 返回任何人进行的调查,但 @user.survey_taken.surveys_taken 返回由 进行的调查>@用户。关键是merge(self.scoped)

在我接受之前等待进一步的评论..

I think what I'm looking for is this:

class SurveyTaking < ActiveRecord::Base
  def self.surveys_taken
    Survey.joins(:survey_takings).where("survey_takings.state = 'completed'").merge(self.scoped)
  end
end

This way, SurveyTaking.surveys_taken returns surveys taken by anyone, but @user.survey_takings.surveys_taken returns surveys taken by @user. The key is merge(self.scoped).

Waiting for further comments before I accept..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文