使用 Rails 3.1,我有以下内容:
# state.rb
class State < ActiveRecord::Base
belongs_to :country, :touch => true
after_save :count_total_states
private
def count_total_states
total_states = State.where(:country_id => self.country_id).count
Country.where(:id => self.country_id).update_column(:state, total_states)
end
end
# states_controller.rb
class StatesController < ApplicationController
def create
@country = Country.find(params[:country_id])
@state = @country.states.build(params[:state])
@state.position = State.where(:country_id => @country.id).maximum(:position).to_i + 1
@state.save
end
end
当我为 创建新对象时state
,显示以下错误:
NoMethodError in StatesController#create
undefined method `update_column' for #<ActiveRecord::Relation:0x1074b0e58>
返回控制器的方法是什么?请指教。
谢谢。