I want to update status to 1, when user views a message.
i need an active record, to change status to 1 ,where status is 0 and id is current id
Any help is appreciated. i want to change this query to active record.
UPDATE 'course_queries SET status = '1' WHERE course_queries.id =41 and status=0;
Something like this
Using ActiveRecord.
@course_query = CourseQuery.find(params[:id]) #id is 41 from your comment
if @course_query.status == 0
@course_query.status = 1
@course_query.save
end
As Nithin mentioned, ActiveRecord is a rails feature. Thus, the following wouldn't work unless you're using the Ruby on Rails framework.
With that said, you could also try:
@course = CourseQuery.where(id: params[:id], status: 0).each do |q|
q.update(status: 1)
end
If you need it all in one line, you could use:
@course = CourseQuery.where(id: params[:id], status: 0).update_all(status: 1)
You can then change the id or status here dynamically. :)
Since these examples use the update and update_all methods, I'd recommend you read up about them. Here's a great article I've found on the differences: Difference between active record methods – update, update_all, update_attribute, update_attributes
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.