I am developing a system in ROR and I have a news table in the bank, being that there is a highlight column and every time the admin registers another notice as highlight even if there is already one in the bank, the controller has to remove the one that was already featured and put this new. NOTE: The highlighted field is Boolean so it will only change from "true" to "false".
Controller
def create
# @notice = Notice.new(notice_params)
featured = params[:notice][:featured]
category = params[:notice][:category_id]
user = current_user.secretary.prefecture.county_id
if featured = 1
@notice = Notice.where(county_id: user, category_id: category, featured: true)
if @notice.present?
@notice = Notice.find(@notice.id).update(featured: false) rescue nil
end
end
end