Rails 2 中的命名范围

发布于 2024-11-27 18:27:07 字数 260 浏览 1 评论 0原文

我有两个模型.. MemberMemberMeeting

会员有_许多会员_会议。

我在 Member 模型中编写了一个named_scope,以便它对member_meetings 执行加入操作。

现在最困难的部分是,

我有一个布尔列 xyz。我需要检查 count(xyz=true) > 1 在named_scope内

I have two models.. Member and MemberMeeting .

Member has_many member_meetings.

I have written a named_scope in Member model such that it does a join operation on the member_meetings.

Now the hard part,

I have a boolean column xyz. I need to check for count(xyz=true) > 1 inside the named_scope

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

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

发布评论

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

评论(2

梦里的微风 2024-12-04 18:27:07

您确定要为此提供范围吗?我假设您想要一份拥有多个会议且 xyz 为真的所有成员的列表,对吧?在这种情况下,应该这样做:

members = Member.include(:member_meetings)
members.select{ |m| m.member_meetings.select(&:xyz).size > 1 }

Are you sure you want a scope for this? I assume you want a list of all members that have more than one meeting where xyz is true, right? In that case, something like this should do:

members = Member.include(:member_meetings)
members.select{ |m| m.member_meetings.select(&:xyz).size > 1 }
ゃ人海孤独症 2024-12-04 18:27:07

将这个named_scope添加到Member应该可以做到:

named_scope :my_scope, :include => :member_meetings, :conditions => { 'member_meetings.xyz' => true }

Adding this named_scope to Member should do it:

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