当我们运行我们的规范时,我们会不断看到如下警告:
Object#id将被弃用; 使用Object#object_id
有问题的代码是访问ActiveRecord模型的id(显然,这是表中的属性,而不是Ruby VM中的对象实例ID).
有谁知道如何关闭这些特定的警告或以某种方式避免它们?
有没有办法创造这样的条件?
@products = Product.find(:all,
:limit => 5,
:conditions => { :products => { :locale => 'en', :id NOT '1' }, :tags => { :name => ['a','b']})
Run Code Online (Sandbox Code Playgroud)
我想列出所有产品,不包括产品1. Thx.
我是一个铁杆新手,我正试图在带有rails的桌子上进行搜索,而我只是使用我的sql知识来做到这一点.但这似乎不像铁路或红宝石甚至......
有没有更好的方法来做我在下面做的事情?(基本上,只有在填充时才将日期参数传递给sql)
def search(begin_date=nil, end_date=nil)
subject = " and created_at "
if !(begin_date.nil? || end_date.nil?)
where_part = subject + "BETWEEN :begin_date AND :end_date"
else if (begin_date.nil? && end_date.nil?)
where_part = ""
else if(begin_date.nil?)
where_part = subject + " <= :end_date"
else if (end_date.nil?)
where_part = subject + " >= :begin_date"
end
end
end
end
User.joins(places: {containers: {label: :user}}).where("users.id= :user_id "+where_part, user_id: self.id, begin_date:begin_date, end_date:end_date).group(...).select(...)
end
Run Code Online (Sandbox Code Playgroud)
编辑
user.rb
has_many :containers
has_many :user_places
has_many :places, through: :user_places
has_many :labels
Run Code Online (Sandbox Code Playgroud)
place.rb
has_many :containers …Run Code Online (Sandbox Code Playgroud) 什么是挽救异常并继续处理的正确方法?我有一个具有文件夹和项目的应用程序,通过名为folders_items的连接表具有habtm关系.该表具有唯一约束,确保没有重复的项目/文件夹组合.如果用户尝试多次将项目添加到同一文件夹,我显然不希望添加其他行; 但我也不想停止处理.
当违反唯一约束时,Postgres会自动抛出异常,因此我尝试在控制器中忽略它,如下所示:
rescue PG::Error, :with => :do_nothing
def do_nothing
end
Run Code Online (Sandbox Code Playgroud)
这在单次插入时工作正常.控制器执行渲染,状态代码为200.但是,我有另一种方法在循环中执行批量插入.在该方法中,控制器在遇到第一个重复行时退出循环,这不是我想要的.起初,我认为循环必须包含在正在回滚的事务中,但事实并非如此 - 重复插入之前的所有行.我希望它只是忽略约束异常并移动到下一个项目.如何防止PG :: Error异常中断此操作?
postgresql activerecord ruby-on-rails ruby-on-rails-3 rails-activerecord
我正在尝试调试一些代码.一个奇怪的部分是before_save回调被调用两次,尽管我只打算保存一次对象.
为了追踪这种情况,我在课堂上定义了这些方法:
%w[save save!].each do |method_name|
define_method(method_name) do |*args|
puts "who called '#{method_name}'? #{caller.first}"
super(*args)
end
end
Run Code Online (Sandbox Code Playgroud)
从这个输出中,我只看到一个持久性调用.
我相信,save和save!是导致ActiveRecord的持久化对象的唯一方法.据我所知,其他持久性方法依赖于这两者之一; 例如,update_attributes电话save,update_attributes!电话save!等
是否有任何方法持有ActiveRecord对象而不调用save或save!?
(我正在使用ActiveRecord 3.2.13)
查看3.2.13的Rails源代码,保存并保存!调用私有方法create_or_update来完成他们的工作.我曾经git grep寻找其他事件,create_or_update并且只找到了测试和callbacks.rb包装它的东西.
create_or_update反过来,依赖klass.connection.update和self.class.unscoped.insert.
所以问题可能是除了create_or_update持久存在ActiveRecord对象之外还有什么.
我有一个where看起来像这样的Rails ActiveRecord :
Things.where('blahs.foo_id = ? OR bar_id = ?', user.id, user.id)
Run Code Online (Sandbox Code Playgroud)
我想user.id用它代替两个?人.有没有办法像我一样不重复它?
我在Rails 3中工作并且有一个包含多个子表的表,例如
class Foo < ActiveRecord::Base
has_many :things
has_many :items
has_many :widgets
end
class Thing < ActiveRecord::Base
belongs_to :foo
end
class Item < ActiveRecord::Base
belongs_to :foo
end
class Widget < ActiveRecord::Base
belongs_to :foo
end
Run Code Online (Sandbox Code Playgroud)
有一种简单的方法让我检查给定的Foo是否在一个或多个表中有子记录?基本上,有更好的方法来做到这一点:
if !foo.things.empty? or !foo.items.empty? or !foo.widgets.empty?
puts "This foo is in use!"
emd
Run Code Online (Sandbox Code Playgroud) 在我的场景中,我有一个BlogPost模型has_and_belongs_to_many :categories.我想筛选出属于特定的类别,同时仍允许未分类的博客文章(因此需要一个LEFT OUTER JOIN)博客文章.
我希望这可行:
BlogPost.active.includes(:categories).where.not(categories: { id: [1, 2, 3] })
Run Code Online (Sandbox Code Playgroud)
但它没有正确过滤,因为它将条件放在查询的末尾,在LEFT OUTER JOIN之外(请参阅此SO问题/答案,原因是:SQL join:where子句与on子句)
这有效,但很难看:
BlogPost.active.joins("LEFT OUTER JOIN blog_posts_categories
ON blog_posts_categories.blog_post_id = blog_posts.id
AND blog_posts_categories.category_id NOT IN(1, 2, 3)")
Run Code Online (Sandbox Code Playgroud)
是否有一种ActiveRecord友好的方式来添加条件到LEFT OUTER JOIN的ON子句而不用我的方式手动输入它?
我正试图从实例化对象中加载关联,即不是与父对象一起读取关联...
User.includes(:characters).first
Run Code Online (Sandbox Code Playgroud)
...推迟直到我确定它真的需要并执行以下操作:
u = User.first
# Other stuff ...
u.eager_load(:characters)
Run Code Online (Sandbox Code Playgroud)
在Rails 3中,我用这种方法增强了ActiveRecord:
def eager_load(*args)
ActiveRecord::Associations::Preloader.new(self, *args).run
end
Run Code Online (Sandbox Code Playgroud)
它工作得很好.Rails 4改变了这一部分,我将方法更新为:
def eager_load(*args)
ActiveRecord::Associations::Preloader.new.preload(self, *args)
end
Run Code Online (Sandbox Code Playgroud)
不幸的是,它现在做了一些奇怪的事情.看一看:
2.1.2 :001 > u = User.first
[2015-01-06 23:18:03] DEBUG ActiveRecord::Base : User Load (0.3ms) SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
=> #<User id: 1, ...>
2.1.2 :002 > u.eager_load :characters
[2015-01-06 23:18:07] DEBUG ActiveRecord::Base : Character Load (0.2ms) SELECT `characters`.* FROM `characters` WHERE `characters`.`user_id` IN (1)
[2015-01-06 23:18:07] DEBUG ActiveRecord::Base …Run Code Online (Sandbox Code Playgroud) 我在升级到rails 4.2.1时遇到了弃用错误, Modifying already cached Relation. The cache will be reset. Use a cloned Relation to prevent this warning.
我正在尝试运行的操作获得已登录的月份用户数.
我的测试很简单:
get :page
expect(response).to be_success
Run Code Online (Sandbox Code Playgroud)
控制器动作:
def page
@months = {}
(0..11).each do |month|
@months[month] = User.group(:usergroup).number_first_logged_in(Date.new(Date.today.year,month+1, 1))
end
end
Run Code Online (Sandbox Code Playgroud)
用户模型
class Model < ActiveRecord::Base
...
def number_first_logged_in(month)
where(first_logged_in_at: month.beginning_of_month..month.end_of_month).count
end
end
Run Code Online (Sandbox Code Playgroud)
我意识到我运行了几乎相同的查询12次,但参数不同.当用户未分组时,此方法在别处使用.如何按照弃用警告中的建议"克隆"关系?
我不想简单地忽略这一点,因为它在运行测试时填满了我的屏幕,这不是很有帮助
ruby-on-rails rails-activerecord rails-4-2-1 adequate-record
activerecord ×3
ruby ×2
sql ×2
conditional ×1
join ×1
postgresql ×1
rails-4-2-1 ×1
rspec ×1
warnings ×1
where ×1