我在我的项目中使用了rails_admin gem和devise gem,postgresql for database。安装了Rails admin gem之后我将看到localhost:3000 / admin然后它会抛出错误 PG :: UndefinedColumn at / 错误:列customers.active不存在。我已经使用过用户和客户模型。我还创建了他们两个模型之间的关系。我认为这是关联问题,但我怎么能修复它我不知道
这是我的用户模型
class User < ActiveRecord::Base
has_one :customer, inverse_of: :user
accepts_nested_attributes_for :customer, :allow_destroy => true
end
这是我的客户模型
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
这是我的rails_admin配置
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
这是我的路线文件
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: user@example.com
** 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
感谢您的帮助!
答案 0 :(得分:1)
检查您是否已运行迁移,以及迁移文件中是否有活动列。
要重做您的迁移,请使用:
rake db:reset db:migrate
修改强>
现在我了解您的问题,active
方法属于user
而非customer
。
首先,我认为您应该查看有关default_scope
的{{3}}。
然后,为了达到你想要的效果,你可以使用像这样的客户活动方法:
def active
self.user.active
end