实现 has_many :through

发布于 2025-01-08 12:28:36 字数 1112 浏览 0 评论 0原文

我正在考虑使用“has_many :through”创建以下模型:

class Contract < AR::Base
  has_many :clientlines
  has_many :codelines
  has_many :clients, :through => :clientlines
  has_many :codes, :through => :codelines
end

class clientlines < AR::Base
  belongs_to :contract
  belongs_to :client
end

class Client < AR::Base
  has_many :clientlines
  has_many :contracts, :through => :clientlines
end

class codeline < AR::Base
  belongs_to :contract
  belongs_to :code
  units_alloc -------**I would like to add this attribute after this intermediate
 end                 has been created?

class Code < AR::Base
  has_many :codelines
  has_many :contracts, :through => :codelines
end

例如,我是否首先使用“rails generated model Contract authnum:string, client_id:integer, st_date:date, end_date:date”创建模型。 然后填写迁移之前的所有关联?

另外,我的理解是,使用 has_many :through 关联时,所有连接表都是由 Rails 自动创建的。什么时候会发生这种情况?

最后,如 ** 所示,我可以在代码行中包含此属性吗?是否创建“rails 生成迁移 add_units_alloc_to_codelinesunits_alloc:number”以便将此属性添加到联接表?我还想知道如何声明该数字超过小数点后两位?

如果您有时间和意愿,您能否对我提出的数据库设计发表评论?

谢谢。

I am thinking of creating the following models using 'has_many :through':

class Contract < AR::Base
  has_many :clientlines
  has_many :codelines
  has_many :clients, :through => :clientlines
  has_many :codes, :through => :codelines
end

class clientlines < AR::Base
  belongs_to :contract
  belongs_to :client
end

class Client < AR::Base
  has_many :clientlines
  has_many :contracts, :through => :clientlines
end

class codeline < AR::Base
  belongs_to :contract
  belongs_to :code
  units_alloc -------**I would like to add this attribute after this intermediate
 end                 has been created?

class Code < AR::Base
  has_many :codelines
  has_many :contracts, :through => :codelines
end

Do I first create the models with 'rails generate model Contract authnum:string, client_id:integer, st_date:date, end_date:date' for example.
Then fill in all of the associations before the migrations?.

Also, my understanding is that all of the join tables are created automatically by rails when using the has_many :through association. When does that happen?

Lastly, as indicated by the **, can I have this attribute in codelines, and do I create a 'rails generate migration add_units_alloc_to_codelines units_alloc:number' in order to add this attribute to the join table? I was also wondering how I declare the number to be too two decimal places?

If you have the time and inclination could you please comment on my proposed design for my database?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年华零落成诗 2025-01-15 12:28:36
  1. 通过使用 has_many :through 您可以使用第三个模型来连接其他两个模型,因此 Rails 不会自动构建该模型,您可以自己构建它并引用该模型的外键其他两个模型。

  2. 不要用复数来命名你的模型,总是用单数。如果你不能用单数来命名它们,那么你就错了

  3. 创建模型的顺序不重要太多(rails生成一些迁移,你可以轻松修改它们稍后)

  4. units_alloc 属性,只需将其添加到模型中即可创建它,就这么简单!

  5. 对于 2 个小数,使用类似 t.decimal :amount, : precision =>; 6、:刻度=> 2 在您的迁移中(该示例为您提供了 6 位数字和 2 位小数)

  6. 阅读 Rails指南,它确实会帮助您摆脱很多麻烦

  1. by using has_many :through you use a third model that makes the connection between the other two, so rails doesn't automatically build that model, you build it yourself and reference the foreign keys for the other two models.

  2. don't name your models at their plural, always singular. if you can't name them at singular, you're doing it wrong

  3. The order in which you create your models shouldn't matter too much (rails generates some migrations which you can easily modify later)

  4. That units_alloc attribute, just add it to the model when you create it, simple as that!

  5. for 2 decimals use something like t.decimal :amount, :precision => 6, :scale => 2 in your migration (that example gives you 6 digits and 2 decimals)

  6. Read the Rails Guides, it will really help you get out a lot of trouble

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文