A memo about .build and build_xxx in Ruby on Rails.
The build method can be found by googling, but I wondered “What is build_xxx?” and looked it up.
build_xxx is syntax that can be used when associations are defined with has_one,
tag = @article.tag.build
Instead of writing this,
tag = @article.build_tag
You can also write it like this.
By the way, build is convenient because it automatically sets the foreign key for the parent model, unlike the new method!
Read more details here:
・build (ActiveRecord::Relation) - APIdock
・Ruby on Rails Memo – Ride On Rails » Blog Archive » Difference between ActiveRecord’s new and build
That’s all from the Gemba.