关联问题
我对某个协会有疑问:
营 :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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该能够执行类似的操作(假设您在第二个查询中只需要
@bseniorleads
实例变量):我还没有检查过,但我认为它应该让您非常接近。
您可能想查看这两个 Rails 指南,它们无疑帮助我更好地理解 ActiveRecord 关联和查询:
You should be able to do something like this (assuming you only needed the
@bseniorleads
instance variable in the second query):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: