gpt4 book ai didi

ruby-on-rails - rails : Increment a column and update attributes

转载 作者:数据小太阳 更新时间:2023-10-29 08:29:56 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com