Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.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/5/ruby/20.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/1/cassandra/3.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_Associations - Fatal编程技术网

Ruby on rails 二者对同一模型有许多联系

Ruby on rails 二者对同一模型有许多联系,ruby-on-rails,ruby,associations,Ruby On Rails,Ruby,Associations,我有一个博客模型,有很多帖子,其中一些帖子是顶级帖子。我的意图是做如下的事情 class Blog has_many :posts has_many :top_posts, class_name: 'Post' end class Post belongs_to :blog end 正如您所看到的,posts和top posts是相同的对象,但是top posts的集合不同于posts的集合。有些职位也是顶级职位,但其他职位不是 问题是当我尝试执行blog.top_posts时,

我有一个博客模型,有很多帖子,其中一些帖子是顶级帖子。我的意图是做如下的事情

class Blog
  has_many :posts
  has_many :top_posts, class_name: 'Post'
end

class Post
  belongs_to :blog
end
正如您所看到的,posts和top posts是相同的对象,但是top posts的集合不同于posts的集合。有些职位也是顶级职位,但其他职位不是


问题是当我尝试执行
blog.top_posts
时,它返回与
blog.posts
相同的集合,这些都是来自该博客的帖子。我希望
blog.top_posts
只返回我通过
blog.top_post_id关联为博客top posts的帖子。我会假设,就像David在评论中问你的那样,你有特定的帖子

举个例子,如果一个帖子有50个喜欢,或者1000个视图,或者数据库中的wathever属性使一个帖子成为热门帖子,那么这个帖子可能会成为热门帖子

如果数据库中没有任何持久的条件,则不能使用active record获取top_post。 无论如何,如果您有,您应该使用范围:

 class Blog
  has_many :posts

 end

class Post
  belongs_to :blog
  scope :top_posts, -> { where(views > 1000 ) }  #or any conditions base on your criterias
end
你可以简单地得到它们:

    blog = Blog.first
    blog.posts.top_posts # => [top posts belonging to this blog]
这是一个基于假设的答案

范围文件:
我会假设,就像大卫在评论中问你的那样,你有一个特别的帖子

举个例子,如果一个帖子有50个喜欢,或者1000个视图,或者数据库中的wathever属性使一个帖子成为热门帖子,那么这个帖子可能会成为热门帖子

如果数据库中没有任何持久的条件,则不能使用active record获取top_post。 无论如何,如果您有,您应该使用范围:

 class Blog
  has_many :posts

 end

class Post
  belongs_to :blog
  scope :top_posts, -> { where(views > 1000 ) }  #or any conditions base on your criterias
end
你可以简单地得到它们:

    blog = Blog.first
    blog.posts.top_posts # => [top posts belonging to this blog]
这是一个基于假设的答案

范围文件:
我会假设,就像大卫在评论中问你的那样,你有一个特别的帖子

举个例子,如果一个帖子有50个喜欢,或者1000个视图,或者数据库中的wathever属性使一个帖子成为热门帖子,那么这个帖子可能会成为热门帖子

如果数据库中没有任何持久的条件,则不能使用active record获取top_post。 无论如何,如果您有,您应该使用范围:

 class Blog
  has_many :posts

 end

class Post
  belongs_to :blog
  scope :top_posts, -> { where(views > 1000 ) }  #or any conditions base on your criterias
end
你可以简单地得到它们:

    blog = Blog.first
    blog.posts.top_posts # => [top posts belonging to this blog]
这是一个基于假设的答案

范围文件:
我会假设,就像大卫在评论中问你的那样,你有一个特别的帖子

举个例子,如果一个帖子有50个喜欢,或者1000个视图,或者数据库中的wathever属性使一个帖子成为热门帖子,那么这个帖子可能会成为热门帖子

如果数据库中没有任何持久的条件,则不能使用active record获取top_post。 无论如何,如果您有,您应该使用范围:

 class Blog
  has_many :posts

 end

class Post
  belongs_to :blog
  scope :top_posts, -> { where(views > 1000 ) }  #or any conditions base on your criterias
end
你可以简单地得到它们:

    blog = Blog.first
    blog.posts.top_posts # => [top posts belonging to this blog]
这是一个基于假设的答案

范围文件:
s

使用
的问题有很多
是它只是将
Blog
对象的ID附加到每个
Post
对象,因此当您调用
Blog.posts
Blog.top\u posts
时,它会执行一个SQL查询,查找ID=Blog.ID的
posts,因此,您会得到两次相同的列表

我建议您使用一个包含多篇文章的
,然后对每个文章返回的列表进行排序,如果您希望避免排序,我建议如下:

class Blog
  has_many :posts

  def initialize
    @top_posts = []
  end

  def add_top_post(post)
    if self.posts.include?(post)
      @top_posts << post
    end
  end
end

class Post
  belongs_to :blog
end
class博客
有很多帖子吗
def初始化
@顶级职位=[]
结束
def添加顶部立柱(立柱)
如果是自我。帖子。包括?(帖子)

@top_posts使用
的问题有很多
是它只是将
Blog
对象的ID附加到每个
Post
对象,因此当您调用
Blog.posts
Blog.top_posts
时,它会执行一个SQL查询,查找ID=Blog.ID
帖子,因此,您会得到两次相同的列表

我建议您使用一个包含多篇文章的
,然后对每个文章返回的列表进行排序,如果您希望避免排序,我建议如下:

class Blog
  has_many :posts

  def initialize
    @top_posts = []
  end

  def add_top_post(post)
    if self.posts.include?(post)
      @top_posts << post
    end
  end
end

class Post
  belongs_to :blog
end
class博客
有很多帖子吗
def初始化
@顶级职位=[]
结束
def添加顶部立柱(立柱)
如果是自我。帖子。包括?(帖子)

@top_posts使用
的问题有很多
是它只是将
Blog
对象的ID附加到每个
Post
对象,因此当您调用
Blog.posts
Blog.top_posts
时,它会执行一个SQL查询,查找ID=Blog.ID
帖子,因此,您会得到两次相同的列表

我建议您使用一个包含多篇文章的
,然后对每个文章返回的列表进行排序,如果您希望避免排序,我建议如下:

class Blog
  has_many :posts

  def initialize
    @top_posts = []
  end

  def add_top_post(post)
    if self.posts.include?(post)
      @top_posts << post
    end
  end
end

class Post
  belongs_to :blog
end
class博客
有很多帖子吗
def初始化
@顶级职位=[]
结束
def添加顶部立柱(立柱)
如果是自我。帖子。包括?(帖子)

@top_posts使用
的问题有很多
是它只是将
Blog
对象的ID附加到每个
Post
对象,因此当您调用
Blog.posts
Blog.top_posts
时,它会执行一个SQL查询,查找ID=Blog.ID
帖子,因此,您会得到两次相同的列表

我建议您使用一个包含多篇文章的
,然后对每个文章返回的列表进行排序,如果您希望避免排序,我建议如下:

class Blog
  has_many :posts

  def initialize
    @top_posts = []
  end

  def add_top_post(post)
    if self.posts.include?(post)
      @top_posts << post
    end
  end
end

class Post
  belongs_to :blog
end
class博客
有很多帖子吗
def初始化
@顶级职位=[]
结束
def添加顶部立柱(立柱)
如果是自我。帖子。包括?(帖子)
@顶部帖子范围如何?

班级博客 有很多帖子吗 结束 班岗 属于:博客 作用域:top,->{where top\u post:true} 结束 #这得到了所有的高层职位 blog.posts.top 示波器怎么样?

班级博客 有很多帖子吗 结束 班岗 属于:博客 范围:top,->{where top\u post:true