Rails - 如何组织他们的模型代码?

时间:2022-07-22 20:37:20

I'm sorry if this seems over simplified but I'm wondering quite specifically what the general consensus is on how model code should be organized.

如果这看起来过于简化,我很抱歉,但我非常清楚地知道关于如何组织模型代码的一般共识。

Association declarations, if you think certain association types should be declared before others, accepts_nested_resource declarations, validations, custom validations, protected and private methods, module inclusions, everything and anything.

关联声明,如果您认为某些关联类型应该在其他关联之前声明,则接受_ented_resource声明,验证,自定义验证,受保护和私有方法,模块包含,所有内容和任何内容。

I've heard of weird gothcas that can occur with the ordering of different model callbacks and associations but have never seen them all really documented anywhere.

我听说过奇怪的gothcas可能会出现不同的模型回调和关联的排序,但从来没有看到它们都记录在任何地方。

Obviously there will be some disagreement and there is probably not one true way but that's to be expected. I'm very curious to hear how other people organize their code. Thanks.

显然会有一些分歧,可能没有一种真正的方法,但这是可以预料的。我很好奇听到其他人如何组织他们的代码。谢谢。

1 个解决方案

#1


2  

I use this simple three-step order:

我使用这个简单的三步顺序:

1) Relationships 2) Validations 3) Methods

1)关系2)验证3)方法

I don't think I would ever put anything after the methods or before the relationships.

我不认为我会在方法之后或关系之前放任何东西。

#1


2  

I use this simple three-step order:

我使用这个简单的三步顺序:

1) Relationships 2) Validations 3) Methods

1)关系2)验证3)方法

I don't think I would ever put anything after the methods or before the relationships.

我不认为我会在方法之后或关系之前放任何东西。