处理预先加载和未保存的关联记录

时间:2016-04-28 07:19:27

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord eager-loading

如何处理无法将相关记录保存到数据库的场景?

如果我有

-(void)viewWillAppear:(BOOL)animated{
    if(!self.isViewExist){
        self.isViewExist = YES; // check it is the first time you go to this ViewController -> don't reload anything
    }else{
        [self.tableView reloadData]; // to reload the tableview data
        // or your can refresh anything in your ViewController as you want
    }
}

然后为急切加载添加class Blog < ActiveRecord::Base has_many :posts end class Post < ActiveRecord::Base has_many :comments end 语句会丢弃未保存的记录:

includes

我可以省略blog = Blog.new blog.posts << Post.new blog.posts # => Return value contains one post blog.posts.includes(:comments) # => Empty result 声明,但如果我的代码与评论互动并且博客和帖子已保存,则表示代码速度较慢。

我使用的是Rails 4.1。

0 个答案:

没有答案