rails admin column does not exist

﹥>﹥吖頭↗ 提交于 2019-12-11 06:55:59

问题


I'm getting a column does not exist when editing a user in rails admin. The relationship between sales and users is user has_many :sales, and sale belongs_to buyer and another belongs_to for seller, written out like

belongs_to :seller, :class_name=>User, :foreign_key=>:seller_id

ActionView::Template::Error (PG::Error: ERROR:  column sales.user_id does not exist
LINE 1: SELECT "sales".* FROM "sales"  WHERE "sales"."user_id" = 14
                                             ^
: SELECT "sales".* FROM "sales"  WHERE "sales"."user_id" = 14):
    1: = rails_admin_form_for @object, :url => edit_path(@abstract_model, @object.id), :as => @abstract_model.param_key, :html => { :method => "put", :multipart => true, :class => "form-horizontal denser", :data => { :title => @page_name } } do |form|
    2:   = form.generate :action => :update

I tried editing the config file for the model (initializers/rails_admin) to not show anything concerning sales on the show page. I did this by adding just a few test fields (and leaving out anything to do with sales) to the list do, show do and edit do blocks for the User model.

I'm not too sure what to edit so that rails admin doesn't try to look for a user id within a sales table.

Any ideas and especially answeres are appreciated!


回答1:


You need to specify the foreign_key on the has_many/has_one association.

Check out the Rails Docs:

:foreign_key

Specify the foreign key used for the association. By default this is guessed to be the name of this class in lower-case and "_id" suffixed. So a Person class that makes a has_many association will use "person_id" as the default :foreign_key



来源:https://stackoverflow.com/questions/11873011/rails-admin-column-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!