Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is build_ Syntactic Sugar?

I'm learning rails, and can't find a good way to search for this, as Google doesn't like underscores. What's build_* do as a prefix? Does it make an initializer for whatever model you attach it to?

like image 434
Elijah Murray Avatar asked Oct 20 '25 10:10

Elijah Murray


1 Answers

It is related to associations. You can use it on a child class's object to build the parent association. You can't use it the other way around.

class Puppy
  belong_to :dog
  attr_accessor :name
end

class Dog
  has_many :puppies
  attr_accessor :name
end

p = Puppy.new(name: "baby")
p.build_dog(name: "John)
like image 67
JaTo Avatar answered Oct 22 '25 00:10

JaTo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!