訪問與用戶關聯的查詢不返回查詢列表,而是返回空白數組。Rails has_many關聯返回空數組
例如:
user = User.first
=> [valid user]
i = Inquiry.first
=> [valid inquiry]
i.user = user
user.inquiries
=> #<ActiveRecord::Associations::CollectionProxy []>
我的三個型號:
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :clients
has_many :inquiries
end
client.rb
class Client < ActiveRecord::Base
belongs_to :user
validates :user_id, :name, presence: true
end
inquiry.rb
class Inquiry < ActiveRecord::Base
belongs_to :user
validates :user_id, :client_name, presence: true
end
客戶端和查詢模型在遷移文件中有t.references,index:true。
如何/你在哪裏創建查詢? – mdenomy
現在我正在軌道控制檯中創建它們。所以我正在做類似的事情,我= Inquiry.new,i.name =「Joe」,i.user = user,i.save。 – Casey