When do I need to use build when creating new records

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

When do I need to use build when creating new records

fugee ohu
I can create a new record from the model on the right side of a has_many statement Why user the model on the left plus . plus the model on the right plus build The reason I'm asking is because I came across this piece of code below As you can see it's a little primitive placing the seller's name in the auction model instead of leaving it alone and rendering in views from the records in User I'm gonna remove seller_name from the table

   @auction = current_user.auctions.build(params[:auction])
    @auction.seller_name = current_user.name

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/83cebab4-47be-407e-b12b-298cc06459cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: When do I need to use build when creating new records

Douglas Lovell
The `build` method constructs the ORM object without validating or saving. It provides an opportunity to configure attributes before saving, before validating. If you feel good about the `params[:auction]`, have them filtered with `require` and `permit`, and aren't going to groom the record any further, then `create` or `create!` will serve. When using `create` it's necessary to check the return value for failures. With `create!` you'll have the `ActiveRecord::RecordInvalid` or other exception to capture on failure.

On Sunday, March 31, 2019 at 6:39:40 PM UTC-3, fugee ohu wrote:
I can create a new record from the model on the right side of a has_many statement Why user the model on the left plus . plus the model on the right plus build The reason I'm asking is because I came across this piece of code below As you can see it's a little primitive placing the seller's name in the auction model instead of leaving it alone and rendering in views from the records in User I'm gonna remove seller_name from the table

   @auction = current_user.auctions.build(params[:auction])
    @auction.seller_name = <a href="http://current_user.name" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fcurrent_user.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNE4EF1iVjJ56O6ix_8r1Yt8ofi1NQ&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fcurrent_user.name\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNE4EF1iVjJ56O6ix_8r1Yt8ofi1NQ&#39;;return true;">current_user.name

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b8c64601-a5f9-445d-ba1d-f1e8c2b0b8c0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.