如何使用不依赖于数据库的条件创建可链接方法(范围)

发布于 2024-11-11 14:20:48 字数 774 浏览 0 评论 0原文

我有一个模型Item,它与其自身有关系。

class Item < ActiveRecord::Base
  has_many :subitems, :class_name => "Item", :foreign_key => "superitem_id"
  belongs_to :superitem, :class_name => "Item"
end

我想查询所有有父项的项目。首先,我尝试检查parent_id是否存在 Item.where("superitem_id != ?", false) 或类似的内容。但这不起作用。尽管该项目具有 superitem_id,但 superitem 可能已被销毁。所以我必须用类方法来做到这一点,

def self.with_superitems
  items = []
  self.find_each do |i|
    items << i if i.superitem
  end
  return items
end

但这使得链接变得不可能,我想用类似的方法链接它,比如

def self.can_be_stored
  items = []
  self.find_each do |i|
    items << i if i.can_be_stored?
  end
  return items
end

是否可以使用作用域实现相同的结果? 或者你会做什么?

I have a model Item, which has a relation to itself.

class Item < ActiveRecord::Base
  has_many :subitems, :class_name => "Item", :foreign_key => "superitem_id"
  belongs_to :superitem, :class_name => "Item"
end

And I want to query all items which have a parent. Firstly I've tried to check if parent_id is present Item.where("superitem_id != ?", false), or something like this. But it doesn't work. Although that item has superitem_id, superitem can be already destroyed. So I have to do it with class method

def self.with_superitems
  items = []
  self.find_each do |i|
    items << i if i.superitem
  end
  return items
end

But it makes chaining impossible, and I want to chain it with similar methods, like

def self.can_be_stored
  items = []
  self.find_each do |i|
    items << i if i.can_be_stored?
  end
  return items
end

Is it possible to achieve the same results with scopes?
Or what would you do?

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

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

发布评论

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

评论(3

待"谢繁草 2024-11-18 14:20:48

我过去也遇到过类似的问题。有时很难绕过它。我发现了一种出于我的目的的黑客方式,所以希望这会有所帮助......

 ids = []
 self.find_each do |i|
    ids << i.id if i.superitem
 end
Model.where('id in (?)', ids)

I've had a similar issue in the past. It's sometimes difficult to get round it. I found a hack-ish way of doing it for my purposes so hope this will help...

 ids = []
 self.find_each do |i|
    ids << i.id if i.superitem
 end
Model.where('id in (?)', ids)
眼角的笑意。 2024-11-18 14:20:48

在rails 2中,我会这样做,

items = Item.find(:all, :include => [:superitems], :conditions => ["superitems.id is not null"])

rails3相当于这样,

Item.includes([:superitem]).where("superitems.id is not null").all

您可以拉入父级并测试连接的superitem一侧的id字段是否有id。如果没有,那是因为那里没有超级项目(或者,从技术上讲,它可能在那里,但没有 id。但这通常不会发生)。

In rails 2 i would have done this

items = Item.find(:all, :include => [:superitems], :conditions => ["superitems.id is not null"])

the rails3 equivalent of this is

Item.includes([:superitem]).where("superitems.id is not null").all

This way you're pulling in the parent and testing if the id field on the superitem side of the join has an id. if it doesn't, it's because there's no superitem there (or, technically, it could be there but have no id. But this would normally never happen).

友欢 2024-11-18 14:20:48

以下将获取具有父项的所有项目,我不确定当您说“虽然该项目具有 superitem_id,但 superitem 可能已被销毁”时的意思

items = Item.where("superitem_id IS NOT NULL")

The following will get all the items with a parent, I'm not sure what you mean when you say "Although that item has superitem_id, superitem can be already destroyed"

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