主动关系:通过关联检索记录?
我有以下模型:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,您想查找某个用户完成的调查吗?如果是这样,你可以这样做:
而且它看起来像而不是:
你可能想使用范围:
If I'm understanding correctly you want to find completed surveys by a certain user? If so you can do:
Also it looks like instead of:
You may want to use scopes:
我想我正在寻找的是这样的:
这样,
SurveyTaking.surveys_taken
返回任何人进行的调查,但@user.survey_taken.surveys_taken
返回由进行的调查>@用户
。关键是merge(self.scoped)
。在我接受之前等待进一步的评论..
I think what I'm looking for is this:
This way,
SurveyTaking.surveys_taken
returns surveys taken by anyone, but@user.survey_takings.surveys_taken
returns surveys taken by@user
. The key ismerge(self.scoped)
.Waiting for further comments before I accept..