Karthick
Karthick

Reputation: 443

postgresql - rails 4 rails_admin column does not exist

I used rails_admin gem and devise gem,postgresql for database in my project.After installed that Rails admin gem and i going to see localhost:3000/admin then it throws the error PG::UndefinedColumn at / ERROR: column customers.active does not exist. I have used user and customer models.I also created relationship between their two models.I think this is association problem but how can i fix it i don't know

Here my user model

class User < ActiveRecord::Base
  has_one :customer, inverse_of: :user
  accepts_nested_attributes_for :customer, :allow_destroy => true
end

Here my customer model

class Customer < ActiveRecord::Base
  default_scope { where(active: true).joins(:user).order("user.name") } 
  belongs_to :user, inverse_of: :customer
  validates :user, presence: true
end

Here my rails_admin configuration

RailsAdmin.config do |config|

 ### Popular gems integration

 # == Devise ==
 config.authenticate_with do
  warden.authenticate! scope: :user
 end
 config.current_user_method(&:current_user)

 config.actions do
   dashboard                     # mandatory
   index                         # mandatory
   new
   export
   bulk_delete
   show
   edit
   delete
   show_in_app

   ## With an audit adapter, you can add:
   # history_index
   # history_show
 end

end

Here my route file

Rails.application.routes.draw do
    mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
    resources :customers
    devise_for :users,  :controllers => { omniauth_callbacks: 'omniauth_callbacks' }
end
\d customers
                                   Table "public.customers"
    Column     |            Type             |                       Modifiers                        --------+-----------------------------+-----------------------------------------------
   id          | integer         | not null default nextval('customers_id_seq'::regclass)
   profile_photo | character varying(255)      | 
   full_address  | text                        | 
   user_id       | integer                     | default 1
   created_at    | timestamp without time zone | 
   updated_at    | timestamp without time zone | 
 Indexes:
   "customers_pkey" PRIMARY KEY, btree (id)
   "index_customers_on_user_id" btree (user_id)

rake db:reset db:migrate --trace

 ** Invoke db:reset (first_time)
 ** Invoke environment (first_time)
 ** Execute environment
 ** Invoke db:load_config (first_time)
 ** Execute db:load_config
 ** Execute db:reset
 ** Invoke db:drop (first_time)
 ** Invoke db:load_config 
 ** Execute db:drop
 ** Invoke db:setup (first_time)
 ** Invoke db:schema:load_if_ruby (first_time)
 ** Invoke db:create (first_time)
 ** Invoke db:load_config 
 ** Execute db:create
 ** Invoke environment 
 ** Execute db:schema:load_if_ruby
 ** Invoke db:schema:load (first_time)
 ** Invoke environment 
 ** Invoke db:load_config 
 ** Execute db:schema:load
 -- enable_extension("plpgsql")
    -> 0.0240s
 -- create_table("users", {:force=>true})
     -> 0.1112s
 -- add_index("users", ["active"], {:name=>"index_users_on_active", :using=>:btree})
    -> 0.0549s
 -- add_index("users", ["confirmation_token"],    {:name=>"index_users_on_confirmation_token", :unique=>true, :using=>:btree})
  -> 0.0442s
 -- add_index("users", ["deleted_at"], {:name=>"index_users_on_deleted_at", :using=>:btree})
    -> 0.0442s
  -- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true, :using=>:btree})
   -> 0.0442s
  -- add_index("users", ["invitation_token"], {:name=>"index_users_on_invitation_token", :unique=>true, :using=>:btree})
  -> 0.0444s
  -- add_index("users", ["mobile_number"], {:name=>"index_users_on_mobile_number", :using=>:btree})
    -> 0.0440s
  -- add_index("users", ["name"], {:name=>"index_users_on_name", :using=>:btree})
  -> 0.0442s
  -- add_index("users", ["reset_password_token"],  {:name=>"index_users_on_reset_password_token", :unique=>true, :using=>:btree})
   -> 0.0442s
  -- create_table("users_roles", {:id=>false, :force=>true})
    -> 0.0110s
  -- add_index("users_roles", ["user_id", "role_id"], {:name=>"index_users_roles_on_user_id_and_role_id", :using=>:btree})
   -> 0.0332s
  -- create_table("customers", {:force=>true})
     -> 0.0883s
   -- add_index("customers", ["user_id"], {:name=>"index_customers_on_user_id", :using=>:btree})
     -> 0.0442s
  -- initialize_schema_migrations_table()
  -> 0.0666s

   ** Invoke db:structure:load_if_sql (first_time)
   ** Invoke db:create 
   ** Invoke environment 
   ** Execute db:structure:load_if_sql
   ** Invoke db:seed (first_time)
   ** Execute db:seed
   ** Invoke db:abort_if_pending_migrations (first_time)
   ** Invoke environment 
   ** Execute db:abort_if_pending_migrations
   CREATED ADMIN USER: [email protected]
   ** Execute db:setup
   ** Invoke db:migrate (first_time)
   ** Invoke environment 
   ** Invoke db:load_config 
   ** Execute db:migrate
   ** Invoke db:_dump (first_time)
   ** Execute db:_dump
   ** Invoke db:schema:dump (first_time)
   ** Invoke environment 
   ** Invoke db:load_config 
   ** Execute db:schema:dump

Thanks your for help!

Upvotes: 0

Views: 1079

Answers (1)

Paulo Fidalgo
Paulo Fidalgo

Reputation: 22331

Check if you have run your migration, and if in your migration file there is the active column.

For redoing your migrations use:

rake db:reset db:migrate

EDIT

Now I understand your problem, the active method belongs to the user not the customer.

So first I think you should take a look at this post regarding the default_scope. Then to achieve what you want you may use an method active in customer like this:

def active
  self.user.active
end

Upvotes: 1

Related Questions