Rails:增加列并更新属性

The*_*pap 5 ruby ruby-on-rails ruby-on-rails-4

我试图在创建项目时增加列并更新属性:

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
Run Code Online (Sandbox Code Playgroud)

我找到了这个对象:

 CACHE SearchSuggestion Load (0.0ms)  SELECT  "search_suggestions".* FROM "search_suggestions" WHERE "search_suggestions"."term" = 'Rolex' LIMIT $1  [["LIMIT", 11]]
Run Code Online (Sandbox Code Playgroud)

但我收到此错误:

NoMethodError (undefined method `update_attributes' for #<ActiveRecord::Relation []>
Did you mean?  update_all):
Run Code Online (Sandbox Code Playgroud)

有什么想法我可能做错了什么吗?

Dim*_*ski 6

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
Run Code Online (Sandbox Code Playgroud)