AnApprentice
AnApprentice

Reputation: 111080

rails Finding and updating a record

I have a model:

ChatStatus with the following fields (ID, user_id, status, thread_id)

What I want to do is update the user's status from 'unread' to 'read'

What I have to do is:

@chat = current_user.chats.where(:thread_id => 23)

23 came from an AJAX post. I then have to do:

@chat.status = 'read'
@chat.save

That hits the database twice. I'm wondering is there a way to do that all in request to the database? Something like:

@chat = current_user.chats.where(:thread_id => 23).status = 'read'

Thanks

Upvotes: 2

Views: 89

Answers (1)

Ben Lee
Ben Lee

Reputation: 53349

I think this is the syntax you are looking for:

current_user.chats.where(:thread_id => 23).update_all(:status => 'read')

Upvotes: 2

Related Questions