关联问题

发布于 2024-08-18 05:21:51 字数 438 浏览 5 评论 0原文

我对某个协会有疑问:

营 :has_many 士兵

士兵 :has_many 初选

我需要这样做 @bseniorleads=(@user.battalion.soldiers.find(:all, :conditions => ["seniorleader = ?", "Yes"])) 然后 @seniorspouse=(@bseniorleads.primaries.find(:all, :conditions => ["relationship = ?", "Spouse"]

这给了我一个未定义的初选方法,我假设是因为 bseniorleads 是一个数组?

基本上我不知道如何以正确的方式执行此操作,但我需要能够从一个模型中查询满足条件的组,然后获取该结果并找到属于他们的另一个模型中的人员有什么想法吗

?进步。

I am having a problem with an association:

Battalion
:has_many soldiers

Soldiers
:has_many primaries

I need to do this
@bseniorleads=(@user.battalion.soldiers.find(:all, :conditions => ["seniorleader = ?", "Yes"]))
then
@seniorspouse=(@bseniorleads.primaries.find(:all, :conditions => ["relationship = ?", "Spouse"]

This gives me an undefined method for primaries, I assume because the bseniorleads is an array?

Basically I don't know how to do this they right way but I need to be able to query a group from one model that meets a condition and then take that result and find the people from another model that belong to them. Any ideas?

Thanks in advance.

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

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

发布评论

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

评论(1

静赏你的温柔 2024-08-25 05:21:51

您应该能够执行类似的操作(假设您在第二个查询中只需要 @bseniorleads 实例变量):

@senior_spouse = @user.battalion.soldiers.find(
   :all,
   :select => 'primaries.*',
   :joins => [:primaries],
   :conditions => ["seniorleader = ? and primaries.relationship = ?", "Yes", "Spouse"]
)

我还没有检查过,但我认为它应该让您非常接近。

您可能想查看这两个 Rails 指南,它们无疑帮助我更好地理解 ActiveRecord 关联和查询:

You should be able to do something like this (assuming you only needed the @bseniorleads instance variable in the second query):

@senior_spouse = @user.battalion.soldiers.find(
   :all,
   :select => 'primaries.*',
   :joins => [:primaries],
   :conditions => ["seniorleader = ? and primaries.relationship = ?", "Yes", "Spouse"]
)

I haven't checked that yet, but I think it should get you pretty close.

You might want to check out these two rails guides, which certainly helped me better understand ActiveRecord associations and querying:

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