Rails 3 ActiveRecord API: .build method
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开发者_JS百科 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. :)
精彩评论