Tag: activerecord

has_many,belongs_to在活动logging移动轨道中的关系4

我创build了一个User模型,后来又创build了一个Task模型。 我在创build时没有提到它们之间的任何关系。 我明白, User has_many Tasks和Task belongs_to User 。 我需要通过移民来build立他们之间的关系。 我的问题是,build立这种关系的移植生成命令是什么? 任何帮助将不胜感激。

Codeigniter – 使用活动logging时处理错误

我正在为我的codeigniter站点放几个模型,似乎无法find关于如何处理使用Active Record系统时可能发生的错误的文档中的任何单词。 该文档演示了如何执行CRUD以及一些相对涉及的查询,但没有讨论error handling的沿线。 我做了一个快速的谷歌search,它会出现,活动logging类不会抛出exception。 这是这种情况吗? 没有尝试赶上,然后… 那么,如何编码来处理codeigniter中的数据库错误? (连接失败,密钥重复,参照完整性受损,截断,错误的数据types等)

validates_uniqueness_of传递nil或空白(没有allow_nil和allow_blank)

如果值为零或空白,ActiveRecord的唯一性validation程序会跳过validation。 即使我将这两个参数都设置为true(默认行为),我也可以在validation命中之前创build一个零和空白的logging。 我使用默认的SQlite3数据库sqlite3-ruby(1.2.5)。 编辑澄清:如果我将validates_presence_of添加到模型,我会得到预期的结果。 我认为validates_uniqueness_of的默认行为会使这个冗余。 testing用例: rails validation_test cd validation_test/ script/generate Model Thing identification:string rake db:migrate app / models / thing.rb的内容: class Thing < ActiveRecord::Base validates_uniqueness_of :identification end Rails控制台: script/console Loading development environment (Rails 2.3.4) >> Thing.create! => #<Thing id: 1, identification: nil, created_at: "2009-09-26 01:49:32", updated_at: "2009-09-26 01:49:32"> >> Thing.create! :identification => "" => […]

has_and_belongs_to_many,避免连接表中的愚蠢

我有一个非常简单的HABTM模型集 class Tag < ActiveRecord::Base has_and_belongs_to_many :posts end class Post < ActiveRecord::Base has_and_belongs_to_many :tags def tags= (tag_list) self.tags.clear tag_list.strip.split(' ').each do self.tags.build(:name => tag) end end end 现在一切正常,除了我在标签表中得到了大量的重复。 我需要做些什么来避免标签表中的重复(基于名称)?

依赖=>摧毁一个“has_many通过”关联

当使用:through选项时,显然依赖=> destroy将被忽略。 所以我有这个… class Comment < ActiveRecord::Base has_many :comment_users, :dependent => :destroy has_many :users, :through => :comment_users … end …但删除评论不会导致关联的comment_userlogging被删除。 那么推荐的方法是什么,然后,使用时:级联删除:通过? 谢谢

何时使用save !,请创build! 和update_attributes! 在Rails中?

我试图找出什么时候使用砰! 保存和更新logging的版本? 我读过并且听说如果你只保存一条logging或更新一个属性,如果你确信没有什么问题,或者总是在控制器之外使用它们,你就不需要它们了。 我想我是多疑的事情得到保存,然后失败,然后有不完整的数据在数据库中。 目前我正在使用的Rails项目已经完成了超过50%,目前不包含任何爆炸声。 我有一些自定义的方法,我正在调用更新或创build多个logging的模型,并担心它们是否应该进行某种事务。 对不起,如果这似乎分散,但我只是想弄清楚如何正确使用ActiveRecord中的保存function,使我的生活更轻松,最终免费一点点。 谢谢你的时间。

如何显示在Rails控制台中运行的SQL查询?

当我在控制台中运行查询(例如MyModel.where(…)或record.associated_things )时,如何才能看到正在运行的实际数据库查询,以便我可以更好地了解正在发生的事情?

ruby超级关键字

据我所知, super关键字在当前类的超类中调用与当前方法同名的方法。 在autoload方法下面,有一个super调用。 我想知道在哪个超类中,我会find一个名称相同的方法,或者在这里调用super module ActiveSupport module Autoload … def autoload(const_name, path = @@at_path) full = [self.name, @@under_path, const_name.to_s, path].compact.join("::") location = path || Inflector.underscore(full) if @@eager_autoload @@autoloads[const_name] = location end super const_name, location end …. end end module ActiveRecord extend ActiveSupport::Autoload … autoload :TestCase autoload :TestFixtures, 'active_record/fixtures' end 这段代码来自rails master分支。 非常感谢。

使用codeigniter的活动logging语法增加mysql数据库的字段

我有以下php-codeigniter脚本,它尝试使用主动logging语法增加logging的字段: $data = array('votes' => '(votes + 1)'); $this->db->where('id', $post['identifier']); $this->db->update('users', $data); 这会产生以下SQL: "UPDATE users SET votes = '(votes + 1)' WHERE id = '44' ” 哪个不运行,但这个SQL确实做我在找什么: "UPDATE用户SET票= (votes + 1) WHERE id = '44' ”`< – 注意缺乏引号(票+ 1 ) 有谁知道如何使用codeigniter的活动logging语法来实现这种types的查询?

Rails的ActiveRecord查询不等于

Rails 3.2.1 有没有一种方法(没有squeel )使用ActiveRecord的散列语法来构造!=运算符? 像Product.where(id: !params[:id]) 生成SELECT products.* FROM products WHERE id != 5 寻找Product.where(id: params[:id])的反义词 UPDATE 在导轨4中not操作员。 Product.where.not(id: params[:id])