Unnamed
Unnamed

Reputation: 1790

Why is my Rails new column not showing up in the model response on the client?

In rails 4, I generated this model:

social_account.rb

class SocialAccount < ActiveRecord::Base
  belongs_to :company
end

then I realized that I needed an active column. So I created this migration:

20140129134020_add_active_to_social_accounts.rb

class AddSelectedToSocialAccounts < ActiveRecord::Migration
  def change
    add_column :social_accounts, :active, :boolean
  end
end

I then ran rake db:migrate and it worked as expected. Also, I confirmed that the new field was in my model by running rails console.

In the console I ran this:

SocialAccount.find(10).to_json and it returned this as I expected:

SocialAccount Load (5.5ms)  SELECT "social_accounts".* FROM "social_accounts" WHERE "social_accounts"."id" = ? LIMIT 1  [["id", 10]]
 => "{\"id\":10,\"vendor\":\"twitter\",\"created_at\":\"2014-01-30T02:05:56.106Z\",\"updated_at\":\"2014-01-30T02:05:56.106Z\",\"company_id\":1,\"active\":true}" 

The main point being that it includes the active column.

But now in my browser I try this: $.getJSON('/api/social_accounts/10.json').success(function(data){console.log(data)}) but it returns without the active column:

created_at: "2014-01-30T02:05:56.106Z"
id: 10
updated_at: "2014-01-30T02:05:56.106Z"
vendor: "twitter"

Also, my controller looks like this:

social_accounts_controller.rb

class SocialAccountsController < ApplicationController
  before_action :set_social_account, only: [:show, :update, :destroy]
 ...

  # GET /social_accounts/1
  # GET /social_accounts/1.json
  def show
  end
 ...

  private
    def set_user
      @user = User.find(session[:user_id])
    end

    def set_social_accounts_for_user
      set_user
      @social_accounts = @user.company.social_accounts
    end
    # Use callbacks to share common setup or constraints between actions.
    def set_social_account
      set_social_accounts_for_user
      @social_account = @social_accounts.find_by(id: params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def social_account_params
      params.permit(:vendor, :active)
    end
end

Why is the active column not showing up? Do I need to clear some cache? Is it not showing because it's a boolean field?

Upvotes: 0

Views: 1915

Answers (1)

zwippie
zwippie

Reputation: 15515

You need to modify your views (html and json) to display the new attribute.

Upvotes: 2

Related Questions