当我找到一个名为“Fils”的资源时,我遇到了这个错误:
PG::UndefinedColumn: ERROR: column users.contact_id does not exist
LINE 1: SELECT "users".* FROM "users" WHERE "users"."contact_id" = ...
^
: SELECT "users".* FROM "users" WHERE "users"."contact_id" = $1 LIMIT 1
我不明白为什么查询会在user.contact_id
而不是fil.contact_id
上查找(我从轨道开始,但我认为这不是好行为)
schema.rb上的Fil表:
create_table "fils", force: :cascade do |t|
t.integer "contact_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.string "channel"
end
Fil模型:
class Fil < ActiveRecord::Base
before_validation :check_or_connect_messageries, :if =>:new_record?
has_one :contact, class_name: "User", :foreign_key => "contact_id"
has_one :user, class_name: "User", :foreign_key => "user_id"
validates :contact_id, presence: true
validates :user_id, presence: true
before_create :set_channel
after_find :set_channel # des fois que
用户模型:
class User < ActiveRecord::Base
before_validation :check_genres
before_validation :set_image
before_validation :init_attrs
before_save :create_mangopay_bank_account
before_create :set_name
before_create :create_mangopay
after_create :set_centres_interets
after_create :set_preferences_musicales
after_create :check_on_create
after_update :check_on_update
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
has_one :preferences_voyage, dependent: :destroy
has_one :verification, dependent: :destroy
has_many :badges, dependent: :destroy
has_many :favoris , dependent: :destroy
has_many :centres_interets, dependent: :destroy
has_many :preferences_musicales, dependent: :destroy
has_many :recommandations, dependent: :destroy
has_many :reputations, dependent: :destroy
has_many :reservations, dependent: :destroy
has_many :routes, dependent: :destroy
has_many :trajets_reguliers, dependent: :destroy
has_many :vehicules, dependent: :destroy
has_many :notifications, dependent: :destroy
validates :date_naissance, presence: true
validates :first_name, presence: true
validates :last_name, presence: true
attr_accessor :points_communs
attr_accessor :modele # created/booked/archived
attr_accessor :peut_laisser_commentaire
attr_accessor :est_abonne
attr_accessor :est_bloque
attr_accessor :a_vehicule
admin / user.rb:
ActiveAdmin.register User do
menu parent: "Utilisateurs"
permit_params :name, :nickname, :email , :image, :genre,:statut, :fume, :etudes
index do
selectable_column
id_column
column :name
column :nickname
column :email
column "Image" do |user|
image_tag user.image, size: "50"
end
column :genre
column :statut
column :fume
column :etudes
column :profession
actions
end
end
答案 0 :(得分:0)
应该是Fil belongs_to User
和User has_one Fil
,因为查询发生在fils-table中。
可在此处找到一个很好的基本阅读材料:http://guides.rubyonrails.org/association_basics.html#the-has-one-association