【问题标题】:Structuring associations Rails 4构建关联 Rails 4
【发布时间】:2017-06-24 01:38:11
【问题描述】:

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

我正在开发一个 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

我遇到了语法错误和关联错误,并且文档清晰如泥。也许有人可以帮助我正确构建这些关联。

【问题讨论】:

  • 您遵循了哪些文档?
  • guides.rubyonrails.org/association_basics.html

标签: ruby-on-rails associations


【解决方案1】:

您的语法错误出现在 User 类上

改变

 has_many :units, through :lease

到

has_many :unit, through: :lease

或

 has_many :units, :through => :lease

【讨论】:

  • 您的答案需要审核吗?等待 OP 的文档!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多