作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我试图在创建项目时增加一列并更新属性:
def items_create
@categories = Category.all
@item = Item.new(item_params)
@item.store_id = current_store.id
@item.update(account_id: current_store.account.id)
@search_suggestion = SearchSuggestion.where(term: [@item.title])
@search_suggestion.update_attributes(:items_count => +1 )
respond_to do |format|
if @item.save
format.html { redirect_to store_items_index_path(current_store), notice: 'Item was successfully created.' }
format.json { render :template => "stores/items/show", status: :created, location: @item }
else
format.html { render :template => "stores/items/new" }
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
然后我找到了对象:
CACHE SearchSuggestion Load (0.0ms) SELECT "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = 'Rolex' LIMIT $1 [["LIMIT", 11]]
但是我收到这个错误:
NoMethodError (undefined method `update_attributes' for #<ActiveRecord::Relation []>
Did you mean? update_all):
知道我可能做错了什么吗?
最佳答案
where
返回集合。使用 find
或 @search_suggestion.last.update_attributes
@search_suggestion = SearchSuggestion.find_by(term: @item.title.downcase)
if @search_suggestion.present?
@search_suggestion.increment!(:items_count)
end
关于ruby-on-rails - rails : Increment a column and update attributes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47790927/
我是一名优秀的程序员,十分优秀!