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>
コントローラに戻る方法は何ですか?お知らせ下さい。
ありがとう。