English 中文(简体)
具有多种模型的Rails项目组织
原标题:Rails project organization with many models

我正在开发一个rails应用程序,它开始有很多模型。目前有15个,但我正在考虑再添加3-4个,作为类似“标签”的模型(我需要比Acts as Tagable提供的更多功能)。

所以,这让我有点恼火的原因是,15个模型中有7个属于一个共同的父模型。一些是belong_to,还有一些是has_and_bellong_to_many。我正在考虑的所有新车型都属于同一个家长。

所以,我想知道的是,组织这种情况的最佳“Railsy”方式是什么?

我应该/可以开始使用应用程序文件夹中的子文件夹吗?即:app/models/parent/child.rb

我知道这是一个开放式的问题,但我真的很感激关于如何最好地处理模型激增的铁路项目的建议。

谢谢

最佳回答

你可以这样做,我总是这样做:)

只是要注意一些事情:如果你创建了一个文件夹,其中有你的一个模型的名称,它就会失败。实际上,Rails会认为您想要扩展它。

因此,在你的模型文件夹中,用你想要的任何花哨的东西为你的类名做准备。

示例:如果要放置与用户相关的模型,请将它们放在models/user_related/

您必须将其添加到应用程序.rb文件中:

config.autoload_paths += Dir["#{Rails.root.to_s}/app/models/*"].find_all { |f| File.stat(f).directory? }  

这将自动加载models目录中包含的所有文件夹。

问题回答

我认为标题式的回答是个好方法

根据activesupport3.0.11的研究,在选择目录名时需要遵循一些规则:

  1. The name must never match a constant in your system, or LoadError s could occur
  2. The name must be able to be converted to a valid constant name, or NameError s will occur.

Explanation of problem #1

Apneadiving s example of a directory name app/models/user_related works as long as a constant UserRelated is never used in your code. Otherwise a LoadError could potentially happen.

For example, assume there was a model called UserProfile and the first time rails sees the constant is in the UserRelated module. Rails will first try to load a UserRelated::UserProfile constant and failing that a UserProfile constant.

If the user_profile file is at app/models/user_related/user_profile.rb, this matches the underscored path of UserRelated::UserProfile and the file would be loaded expecting to define the UserRelated::UserProfile constant. This would raise the following error because it really defines the UserProfile constant.

Expected app/models/user_related/user_profile.rb to define UserRelated::UserProfile (LoadError)

这种情况发生在活动的支持依赖项代码中。

Explanation of problem #2

Another caveat is the directory name must be able turned into a valid ruby constant name (although to follow #1 the constant should be undefined). For example, if the directory name were app/models/user.related this would result in the following error inside the active_support dependency code:

wrong constant name User.related (NameError)




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签