Ruby on rails 结构关联Rails 4

Ruby on rails 结构关联Rails 4,ruby-on-rails,associations,Ruby On Rails,Associations,这可能是一个相当直截了当的答案,我觉得我应该知道,但偶尔我会遇到这样的事情,让我难堪 我正在开发一个rails应用程序,它要求我创建一个租赁系统 我有一个用户,一栋大楼,一份租约和一套公寓。我现在构建它的方式是: class Buildings < ActiveRecord::Base has_many :units has_many :users end class User < ActiveRecord::Base belongs_to :buildi

这可能是一个相当直截了当的答案,我觉得我应该知道,但偶尔我会遇到这样的事情,让我难堪

我正在开发一个rails应用程序,它要求我创建一个租赁系统

我有一个用户,一栋大楼,一份租约和一套公寓。我现在构建它的方式是:

class Buildings < ActiveRecord::Base

    has_many :units
    has_many :users
end

class User < ActiveRecord::Base

   belongs_to :buildings
   has_many :units, through :lease      
end

class Lease < ActiveRecord::Base

    belongs_to :user
    belongs_to :unit
end

class Unit < ActiveRecord::Base

    belongs_to :building
    belongs_to :user 

    has_one :lease
end
class建筑

我遇到了语法错误和关联错误,文档非常清晰。也许有人能帮助我正确地组织这些联系

您的语法错误在用户类上

改变

 has_many :units, through :lease


您遵循了哪些文档?guides.rubyonrails.org/association_basics.htmly您的答案需要审核?等待OP的文档!
has_many :unit, through: :lease
 has_many :units, :through => :lease