When is touch for a belongs_to in Rails triggered?

From: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html :touch If true, the associated object will be touched (the updated_at/on attributes set to now) when this record is either saved or destroyed. If you specify a symbol, that attribute will be updated with the current time in addition to the updated_at/on attribute. belongs_to :company, :touch => true belongs_to :company, :touch => :employees_last_updated_at

DEPRECATION WARNING: Dangerous query method: Random Record in ActiveRecord >= 5.2

If you want to continue using order by random() then just declare it safe by wrapping it in Arel.sql like the deprecation warning suggests: Model.order(Arel.sql(‘random()’)).first # PostgreSQL Model.order(Arel.sql(‘rand()’)).first # MySQL There are lots of ways of selecting a random row and they all have advantages and disadvantages but there are times when you absolutely must … Read more

How to test that a certain function uses a transaction in Rails and rspec 2

You should look at the problem from a different perspective. Testing whether a function uses a transaction is useless from a behavioral viewpoint. It does not give you any information on whether the function BEHAVES as expected. What you should test is the behavior, i.e. expected outcome is correct. For clarity, lets say you execute … Read more

Iterating through every record in a database – Ruby on Rails / ActiveRecord

Here is the correct syntax to iterate over all User : User.all.each do |user| #the code here is called once for each user # user is accessible by ‘user’ variable # WARNING: User.all performs poorly with large datasets end To improve performance and decrease load, use User.find_each (see doc) instead of User.all. Note that using … Read more

tech