Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 从数据库检索对象(高级数组条件)_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 从数据库检索对象(高级数组条件)

Ruby on rails 从数据库检索对象(高级数组条件),ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我有一个模型,其中标记通过标记关系与其他标记连接。某个标记可能有一个父标记和几个子标记。 这是标记模型: has_many :tagrelationships, :foreign_key => "parent_id", :dependent => :destroy has_many :children, :through => :tagrelationships,

我有一个模型,其中标记通过标记关系与其他标记连接。某个标记可能有一个父标记和几个子标记。 这是标记模型:

    has_many :tagrelationships, :foreign_key => "parent_id",
                                :dependent => :destroy
    has_many :children, :through => :tagrelationships,
                        :source => :child

    has_one :reverse_tagrelationship, :foreign_key => "child_id",
                                      :class_name => "Tagrelationship",
                                      :dependent => :destroy
    has_one :parent,  :through => :reverse_tagrelationship,
                      :source => :parent
标记关系模型:

      belongs_to :child,  :class_name => "Tag"
      belongs_to :parent, :class_name => "Tag"
数据库结构: 标记有以下列:id、name、user\u id、created\u at、updated\u at tagrelationships包含以下列:id、parent\u id、child\u id、created\u at、updated\u at

我无法找到如何选择没有任何父标记的标记。当然,可以选择某个用户的所有标签并在循环中对其进行评估:

    @tags = Tag.where(:user_id => current_user)

    @tags.each do |f|
      if f.parent.nil?
        @roottags << f
      end
    end
@tags=Tag.where(:user\u id=>当前用户)
@每个do | f的标签|
如果f.parent.nil?

@roottags这里有一个可能满足您需求的替代建议

与其创建单独的关系类,不如通过向标记类添加父类id来执行自联接

这通过以下方式简化了关系:

class Tag < ActiveRecord::Base
  has_many :children, :class_name => "Tag", :foreign_key => "parent_id"  
  belongs_to :parent, :class_name => "Tag"

  scope :top_level, where(:parent_id => nil)
end
class标记“Tag”,:foreign\u key=>“parent\u id”
属于\u to:parent,:class\u name=>“标记”
作用域:顶层,其中(:父\u id=>nil)
结束
现在,当您想要查找没有任何父标记的标记(即顶级标记)时,您可以使用命名范围来筛选具有父ID的标记


希望这能有所帮助。

在这两种情况下,我都会收到以下错误消息:“SQLite3::SQLException:没有这样的列:tags.parent\u id:从“tags”中选择COUNT(*),其中(“tags”。“parent\u id”为NULL)“我重写了我的答案,以更好地解决您的问题。当然,您推荐的解决方案可行,但我不想改变这种关系。如果没有更简单的查询解决方案,我将坚持问题中描述的解决方案。