Tag: activerecord

从Rails 3中的Active-record查询中检索sql查询

如何跟踪我的Activerecord方法生成的SQL查询(例如find,where)。

Rails搜索查询关联模型

在railscast#37中,他们展示了我想要实现的简单搜索。 我有以下关联: class Owner < ActiveRecord::Base has_many :dogs end class Dog < ActiveRecord::Base belongs_to :owner end Dog模型具有属性“name”,所有者也是如此。 我需要能够过滤狗的列表,这样如果你输入“Fido”(狗)或“Bob”(所有者)Fido将出现在列表中。 以下是当前搜索的设置方式: model dog.rb: def self.search(search) if search find(:all, :conditions => [‘name LIKE ?’, “%#{search}%”]) else find(:all) end end 这将仅返回与狗名称匹配的搜索词(忽略所有者名称) 我试着把它改成这样的东西: def self.search(search) if search find(:all, :conditions => [‘name LIKE ? or owner.name LIKE ?’, “%#{search}%”, “%#{search}%”]) else […]

如何将ActiveRecordvalidation器作为实例方法调用(ala Sequel)?

我有一个模型需要不同的validation器,具体取决于它的当前状态。 我应该如何为每个实例调用ActiveRecordvalidation器? 我想重用尽可能多的管道,但我不知道如何继续。 class Order < ActiveRecord::Base attr_accessible :state validate :state_specific_validations def state_specific_validations if :paid == self.state # Warning: here be Unicorns… # Wishful thinking… validate_presence_of :paid_at validate_associated :purchaser # Hopeful. What are the validators called internally in Rails? errors << PresenceValidator.new(self, :paid_at).valid? errors << AssociationValidator.new(self, :paid_at).valid? # Plan B # … Hoping for help […]

rails属性名称camelcase问题

我有遗留的Sql Server数据库和Rails应用程序。 数据库中的列名都在PascalCase (FirstName, CustomerId…). 我正在寻找一种在Ruby和PascalCasing数据库中使用underscore_casing的简单方法。 我想在数据库中的FirstName列的Rails中编写first_name 。 camelize和underscore会将字符串转换为所需的大小写 但我正在寻找更通用的东西: ActiveRecord::Base.camelize_table_column_names = true 比如现有的ActiveRecord::Base.pluralize_table_names 就像将rjs模板中的Ruby翻译成JavaScript cameCase约定一样。

如何在Rails中建立相互友谊的模型

我知道之前在Stack Overflow上已经提出了这个问题,但答案并没有以我能解释的方式为我做。 我的一般方法受本教程的启发。 我正在尝试做的是为友好用户创建一个非常简单的模型,通过单个记录在两端创建相同的友谊。 在db级别,我只有一个’friendships’表,它只有一个user_id,一个friend_id和一个is_pending布尔列。 在user.rb中,我将关系定义为: has_many :friendships has_many :friends, through: :friendships 在friendship.rb中,我将关系定义为: belongs_to :user belongs_to :friend, :class_name => ‘User’ 如果我添加友谊,我可以访问如下: > a = User.first > b = User.last > Friendship.new(a.id, b.id) > a.friends => # 这是完美的,但我想要的是也能够像其他方向一样: > b.friends 不幸的是,随着关系的定义,我得到一个空集合。 运行的SQL显示它正在搜索user_id = b.id. 如何指定它还应搜索friend_id = b.id?

Rails find_or_create_by有/无在哪里

假设我有一个名为Task的模型。 我想通过某项任务找到_或_创建。 t = Task.where(done: false).find_or_create_by(title: ‘epic’) 此模型有效,但创建一个title等于epic且done等于false的任务。 我希望查询搜索done等于false,但我不希望新记录done等于false。 我该怎么做?

在Sidekiq作业结束之前释放ActiveRecord连接

这个问题涉及性能和优化问题,以及实现Sidekiq工作的方式。 假设我们有以下工作流程 def执行 # Step 1 do_things_with_activerecord_db_and_get_some_parameters() # Step 2 step2_perform_an_http_request_with_these_parameters_on_unreliable_server() 结束 在下面的情况中,在步骤1中从池中获取ActiveRecord连接,并且只有在SideKiq的ActiveRecord中间件完成(或失败)作业后,SideKiq才会释放该连接。 由于我们执行请求的步骤2中的外部http服务器不可靠,因此http请求可能需要很长时间甚至超时,因此ActiveRecord连接在所有时间都没有锁定,对吧? 所以我的问题是:打电话是否恰当,有用和安全: ActiveRecord的:: Base.clear_active_connections! 在步骤1和步骤2之间,以便作业自己释放资源并使其可用于其他类似的作业? 或者我错过了关于连接池的一些事情? 这种方法也可以应用于redis连接吗? 谢谢提前!

在ActiveRecord查询中包含所有ID

我正在开发一个游戏平台,我有以下(简化)模型: class Game < ActiveRecord:Base has_many :game_players has_many :players, through: :game_players end class Player < ActiveRecord:Base has_many :game_players has_many :games, through: :game_players end class GamePlayer < ActiveRecord:Base belongs_to :game belongs_to :player end 我需要执行一个ActiveRecord查询,查询由特定用户组播放的所有游戏。 例如,给定数据: +———+———–+ | game_id | player_id | +———+———–+ | 10 | 39 | | 10 | 41 | | 10 | 42 […]

Rails 4.0.0 – “没有将nil隐式转换为String”

刚刚安装了一个全新的Rails 4.0.0应用程序,我在http://0.0.0.0:3000上收到此错误( https://gist.github.com/hartator/6404820 ): no implicit conversion of nil into String activerecord (4.0.0) lib/active_record/connection_adapters/sqlite3_adapter.rb:24:in `initialize’ activerecord (4.0.0) lib/active_record/connection_adapters/sqlite3_adapter.rb:24:in `new’ activerecord (4.0.0) lib/active_record/connection_adapters/sqlite3_adapter.rb:24:in `sqlite3_connection’ activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:440:in `new_connection’ activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:450:in `checkout_new_connection’ activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:421:in `acquire_connection’ activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:356:in `block in checkout’ /Users/asa/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize’ activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `checkout’ activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block in connection’ /Users/asa/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize’ […]

使用自定义has_many关系时,nil的未定义方法名称

我正在尝试为我的一个模型创建最简单的has_many关系。 它的定义是这样的: # i know it doesn’t make much sense. I’m using such ridiculous # where case to keep things simple for now has_many :jobs, -> { where(id: 1) }, class_name: SidekiqJob 然而,当我试图以任何方式调用该关系时,例如使用MyModel.last.jobs ,rails抛出: NoMethodError: undefined method `name’ for nil:NilClass from /Volumes/HDD/Users/michal/.rvm/gems/ruby-2.1.1/gems/activerecord-4.0.3/lib/active_record/relation/merger.rb:141:in `block in filter_binds’ 有没有人知道这里出了什么问题? ruby2.1.1 rails 4.0.3 编辑: 原始关联定义: has_many :jobs, (obj) -> […]