神刀安全网

Tapping ActiveRecord Relations

(Yes, I know I said I was probably not going to write here anymore, but while I like Medium for long-form articles, it didn’t quite fit the bill for posts that wanted syntax highlighting. Thus, I’ll probably continue to use the Buckblog for technical articles, and Medium for long-form.)

Not a lot of surprises in today’s post. I just wanted to share a convenient technique that I came across this week.

You’re all familiar with Object#tap , right? It’s terribly useful, in many situations. The example from the documentation is pretty good:

(1..10)              .tap {|x| puts "original: #{x.inspect}"}.   to_a               .tap {|x| puts "array:    #{x.inspect}"}.   select {|x| x%2==0}.tap {|x| puts "evens:    #{x.inspect}"}.   map    {|x| x*x}   .tap {|x| puts "squares:  #{x.inspect}"}.

You’re also probably all familiar with ActiveRecord::Relation , at least indirectly. This is what is returned any time you query a model in ActiveRecord:

User.where("created_at > ?", Date.yesterday) #-> #<ActiveRecord::Relation [...]>

These relation objects allow query operations to be chained together (which you probably also already knew):

q = User.where("created_at > ?", Date.yesterday) #-> #<ActiveRecord::Relation [...]> q = q.limit(5) #-> #<ActiveRecord::Relation [...]> q = q.order(:name) #-> #<ActiveRecord::Relation [...]>

I’ve taken advantage of both Object#tap and the chaining of Relation instances, numerous times, but I don’t think I’ve ever done them together. Recently, though, I saw it done, something like this:

updated_ids = []  Composition.where(author_id: old_id).tap do |relation|   updated_ids.concat(relation.pluck(:id))   relation.update_all(author_id: new_id) end  p updated_ids.uniq

Here, the code collects the ids ( updated_ids ) of all records that are affected by merging one author ( old_id ) into another author ( new_id ). The #tap block just yields the relation, appends the ids of all matching records to the array, and then updates all matching records.

(Could this example be written more effeciently? Probably. Feel free to golf it out.)

I love this about writing software. No matter how familiar you become with the toolbox you’ve been given, someone always manages to combine those tools in ways you wouldn’t have considered.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Tapping ActiveRecord Relations

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址