Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7822497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:56:23+00:00 2026-06-02T07:56:23+00:00

Using Rails 3.1, I have the following: # state.rb class State < ActiveRecord::Base belongs_to

  • 0

Using Rails 3.1, I have the following:

# 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

When I create a new object for state, the following error shows:

NoMethodError in StatesController#create

undefined method `update_column' for #<ActiveRecord::Relation:0x1074b0e58>

What is the method going back to the controller? Please advise.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:56:24+00:00Added an answer on June 2, 2026 at 7:56 am

    This error is because this:

    Country.where(:id => self.country_id)
    

    returns an ActiveRecord::Relation object, not a Country. You probably should do one of these two things instead:

    Country.find(self.country_id).update_column(:state, total_states)
    

    or

    Country.where(:id => self.country_id).first.update_column(:state, total_states)
    

    However I would recommend the first option because it does what you really want, finds a record by the ID. Using Where with a primary key is kind of funky, your objective is to return just one exact match and you know its ID so you should just grab it directly.


    Note: I would also recommend update_attribute instead of update_column, as update_column skips all validation. It’s a good idea to validate unless you really can’t.

    Better yet, the behavior you’re using is actually encapsulated into a standard Rails practice called “counter cache”. See this blog post and this Railscast for a demonstration of a better way to achieve your goal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model: class Advisor < ActiveRecord::Base belongs_to :course end class Course
I am using Rails 3.2. I have following models: Blog Comment User class Blog
I have the following model and methods: class UserPrice < ActiveRecord::Base attr_accessible :price, :purchase_date,
I am using Rails 3 beta 4. I have the following models: class Player
I'm using jQuery with rails and have the following piece of code $('#related').html(<%= render
I have a class User in Rails using Mongoid and Devise. I can't seem
I am using Rails v2.3 If I have a model : class car <
For example I have the following migration class AddStatusField < ActiveRecord::Migration def self.up add_column
I am using rails and have the following scenario: Users have_many lists and each
Using Rails 3.0.3. I have the following route in routes.rb: match user/create_new_password/:reset_password_key =>users#create_new_password, :via=>[:get,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.