Rails 3 ActiveRecord API: .build method

I'm new to Ruby / RoR (out of year), and I noticed that there are several different methods inside RoR or Ruby that basically do the same thing. The only method I want to get is the .build method. when it is effective to use or how to use it in the best light, something like.

Thanks!

+6
source share
1 answer

The .build method is the ActiveRecord method that is used to create a new record based on the has_many relationships in your model.

So let's say:

 User has_many tweets 

Then you can use

 user.tweets.build(tweet_id) 

This will create a new tweet in the tweet table associated with this user. He will also return this object.

You probably want to put params tweet_id in your argument depending on how you implement the application. :)

+5
source

Source: https://habr.com/ru/post/895623/


All Articles