Rails 3.2 ActiveRecord :: InverseOfAssociationNotFoundError中的关联失败

Ben*_*enU 1 ruby-on-rails ruby-on-rails-3.2

我有建筑物,公寓,住宅和用户由于某种原因不能很好地在一起玩.不知道我错过了什么.

class Building < ActiveRecord::Base
  attr_accessible ...

  has_many :apartments, inverse_of: :building
  ...
end


class Apartments < ActiveRecord::Base
  ...
  belongs_to :building, inverse_of: :apartment
  has_many :residences, inverse_of: :apartment
  ...
end


class Residence < ActiveRecord::Base
  ...
  belongs_to :apartment, inverse_of: :residence
  belongs_to :user, inverse_of: :residence
  ...
end


class User < ActiveRecord::Base
...
  has_many :residences, inverse_of: :user
...
end
Run Code Online (Sandbox Code Playgroud)

在rails控制台中,我遇到了反向关联问题:

一类

=>公寓(id:整数,building_id:整数,...,created_at:datetime,updated_at:datetime)

建筑

ActiveRecord :: InverseOfAssociationNotFoundError:找不到/Users/[me]/.rvm/gems/ruby-1.9.3-p194@r3t2/gems/activerecord-3.2.8/中构建(:Building in Building)的反向关联lib/active_record/reflection.rb:246:在`check_validity_of_inverse!'

h.class

=> Residence(id:integer,apartment_id:integer,user_id:integer ...,created_at:datetime,updated_at:datetime)

h.user

ActiveRecord :: InverseOfAssociationNotFoundError:无法从/Users/[me]/.rvm/gems/ruby-1.9.3-p194@r3t2/gems/activerecord-3.2.8/找到用户(:用户居住地)的反向关联lib/active_record/reflection.rb:246:在`check_validity_of_inverse!'

我可以继续 这些类之间的关系似乎被挂起了.我在过去的几个小时里尝试了各种迭代,但最后我称之为"ma ma!" 对任何线索都会非常感激.

Mic*_*ant 7

公寓协会Building是复数,所以你应该添加一个s:

class Apartments < ActiveRecord::Base
  ...
  belongs_to :building, inverse_of: :apartments
  has_many :residences, inverse_of: :apartment
  ...
end
Run Code Online (Sandbox Code Playgroud)