I am fairly new to Ruby/RoR (outside of a year) and I have noticed that there are several different methods inside of RoR or Ruby that basically do the same thing. The one method I am wanting to get some sort of clarification on, is the .build
method. when it is effective to use or how to use it in its best light, sorta thing.
Thanks!
The .build method is an ActiveRecord method which is used to create a new record based on the has_many relationship in your model.
So lets say;
User has_many tweets
Then you can use
user.tweets.build(tweet_id)
This will create a new tweet in the tweets table associated with that user. It will also return that object too.
You probably want to put a params tweet_id in you argument depending on how you are implementing the app. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With