本文介绍了Rails的关联HAS_ONE最新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号:

class Section < ActiveRecord::Base
  belongs_to :page
  has_many :revisions, :class_name => 'SectionRevision', :foreign_key => 'section_id'
  has_many :references

  has_many :revisions, :class_name => 'SectionRevision',
                       :foreign_key => 'section_id'

  delegate :position, to: :current_revision

  def current_revision
    self.revisions.order('created_at DESC').first
  end
end

其中, current_revision 是最近创建的修改。是否有可能把 current_revision 成一个协会这样我就可以执行如查询Section.where(current_revision.parent_section_id ='1')?或者我应该一个 current_revision 列添加的努力几乎或通过关联创建到我的数据库呢?

Where current_revision is the most recently created revision. Is it possible to turn current_revision into an association so I can perform query like Section.where("current_revision.parent_section_id = '1'")? Or should I add a current_revision column to my database instead of trying to create it virtually or through associations?

推荐答案

我明白你想获得其中每个部分的最后修订有部分的parent_section_id = 1;

I understand you want to get the sections where the last revision of each section has a parent_section_id = 1;

我也有类似的情况,首先,这是SQL(请想想的类别为您的部分,员额修订和USER_ID为parent_section_id -sorry,如果我不动了code你的需要,但我必须去):

I have a similar situation, first, this is the SQL (please think the categories as sections for you, posts as revisions and user_id as parent_section_id -sorry if I don't move the code to your need but I have to go):

SELECT categories.*, MAX(posts.id) as M
FROM `categories`
INNER JOIN `posts`
ON `posts`.`category_id` = `categories`.`id`
WHERE `posts`.`user_id` = 1
GROUP BY posts.user_id
having M = (select id from posts where category_id=categories.id order by id desc limit 1)

这是Rails中查询:

And this is the query in Rails:

Category.select("categories.*, MAX(posts.id) as M").joins(:posts).where(:posts => {:user_id => 1}).group("posts.user_id").having("M = (select id from posts where category_id=categories.id order by id desc limit 1)")

这个工作,这是丑陋的,我觉得最好的方法就是剪查询,但如果你有太多的部分,这将是一个问题,而循环槽他们。你也可以把这个查询转换为静态方法,而且,你的第一个想法,有你的部分表内的REVISION_ID将有助于优化查询,但会下降正常化(有时是必要的),你将不得不当一个新的修订该节创建更新此字段(因此,如果你要挣很多版本中修改一个庞大的数据库,也许这将是一个糟糕的主意,如果你有一个缓慢的服务器...)

This works, it is ugly, I think the best way is to "cut" the query, but if you have too many sections that would be a problem while looping trough them; you can also place this query into a static method, and also, your first idea, have a revision_id inside of your sections table will help to optimize the query, but will drop normalization (sometimes it is needed), and you will have to be updating this field when a new revision is created for that section (so if you are going to be making a lot of revisions in a huge database it maybe would be a bad idea if you have a slow server...)

更新我回来嘿嘿,我做了一些测试,并检查了这一点:

UPDATEI'm back hehe, I was making some tests, and check this out:

def last_revision
    revisions.last
end

def self.last_sections_for(parent_section_id)
  ids = Section.includes(:revisions).collect{ |c| c.last_revision.id rescue nil }.delete_if {|x| x == nil}

  Section.select("sections.*, MAX(revisions.id) as M")
         .joins(:revisions)
         .where(:revisions => {:parent_section_id => parent_section_id})
         .group("revisions.parent_section_id")
         .having("M IN (?)", ids)
end

我做了这个查询,并与我的表(希望我命名好参数,可以是从以前一样Rails的查询,但我改变了其优化的查询)工作;注意组;在包括使得它在大型数据集最优的,而抱歉,我无法找到一个方法,使与HAS_ONE的关系,但我会用这个去的,也重新考虑你提到在一开始的领域。

I made this query and worked with my tables (hope I named well the params, it is the same Rails query from before but I change the query in the having for optimization); watch out the group; the includes makes it optimal in large datasets, and sorry I couldn't find a way to make a relation with has_one, but I would go with this, but also reconsider the field that you mention at the beginning.

这篇关于Rails的关联HAS_ONE最新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:51