# What's the "update" equivalent to the "build" method?

**URL:** <https://www.ruby-forum.com/t/whats-the-update-equivalent-to-the-build-method/180482>\
**Category:** Rails\
**Created:** [December 19, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/whats-the-update-equivalent-to-the-build-method/180482 "2009-12-19T21:22:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [December 19, 2009, 9:22pm UTC](https://www.ruby-forum.com/t/whats-the-update-equivalent-to-the-build-method/180482/1 "2009-12-19T21:22:12Z")

</div>

Currently I’m using .build in my Create action so that I have a little  
more control before it gets saved. For instance, I have:  
@comment = current\_user.comments.build(params[:comment])  
@comment.blog\_comment = @comment.blog\_comment[0…140]

to chop the blog\_comment off at 140 characters. Well, that works  
great, but if a user goes to edit that comment, they can then make the  
comment longer than 140 characters since I’m using the following in  
the update action:

```
@comment = Comment.find(params[:id])
if @comment.update_attributes(params[:comment])

```

So, how do I interject and chop off the comment at 140 characters when  
a user decides to update their comment?

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [December 19, 2009, 9:36pm UTC](https://www.ruby-forum.com/t/whats-the-update-equivalent-to-the-build-method/180482/2 "2009-12-19T21:36:51Z")

</div>

On Sat, Dec 19, 2009 at 3:20 PM, AlwaysCharging [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> @comment = Comment.find(params[:id])  
> if @comment.update\_attributes(params[:comment])
> 
> So, how do I interject and chop off the comment at 140 characters when  
> a user decides to update their comment?

One way might be to alter the value first with something like:

@comment = Comment.find(params[:id]  
comment\_attributes = params[:comment]  
comment\_attributes[‘blog\_comment’] &&=  
comment\_attributes[‘blog\_comment’][0…140]  
if @comment.update\_attributes(comment\_attributes)

Something similar could also be done in the create case, in which case  
I’d factor out the two lines calculating comment\_attributes into a  
controller method.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [December 19, 2009, 9:59pm UTC](https://www.ruby-forum.com/t/whats-the-update-equivalent-to-the-build-method/180482/3 "2009-12-19T21:59:41Z")

</div>

Seems like the perfect place for a call back if you’re doing this  
without discriminating.

class Comment \< ActiveRecord::Base  
…  
before\_validation :trim\_blog\_comment

protected  
def block\_comment  
self.blog\_comment = blog\_comment[0…140] if blog\_comment.length \>  
140  
end  
end

Now it’s being trimmed at the model level on all creates/saves/  
updates, no matter where the call to save comes from.  
On Dec 19, 3:20 pm, AlwaysCharging [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @comment = Comment.find(params[:id])
> if @comment.update_attributes(params[:comment])
> 
> ```
> 
> So, how do I interject and chop off the comment at 140 characters when  
> a user decides to update their comment?

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [December 20, 2009, 3:24am UTC](https://www.ruby-forum.com/t/whats-the-update-equivalent-to-the-build-method/180482/4 "2009-12-20T03:24:38Z")

</div>

This is perfect! Thank you Emery.  
I was thinking the same thing, but was trying before\_save filters and  
couldn’t get it to work for the Update action.  
Honestly, I didn’t even know about the before\_validation helper, guess  
I got some more learnin’ to do.  
Thanks again.

And @Rick, thank you for the suggestion as well.

On Dec 19, 3:58 pm, Emery F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 140
> 
> > ```
> > @comment.blog_comment = @comment.blog_comment[0...140]
> > 
> > ```
> > 
> > a user decides to update their comment?  
> > –

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
