I think this question might be better for Rails-talk or Stack Overflow, as
Rails-core is designated for discussion of the core itself.
However, I will answer it anyway....
Unless I'm mistaken, OP is wanting to segregate his models into subfolders.
"The Rails way" philosophy on this, which I will voice objection too as someone
who works primarily on very large apps, is typical to optimize for small apps
and easy-of-entry into the ecosystem but not particularly optimized for larger
apps.
On a larger apps, you could have hundreds and hundreds of models. Separating
into subfolders is perfectly appropriate.
OP -- I think perhaps all you need is to namespace your models themselves. For
example, we have a folder in app/models called platform (a generic name, I know)
All of the models inside of app/models/platform are namespaced using
Platform::
so we have a model called Platform::Block (the file name is
app/models/platform/platform/block.rb)
You can define this using
class Platform::Block < ActiveRecord::Base
# your class code here
end
alternatively, another Ruby syntax for the same thing is
module Platform
class Block < ActiveRecord::Base
# your class code here
end
end
I believe the only way to put models into subfolder is to rename them using
namespaces. Renaming is a good idea, but of course affects things like all the
relationships and STI ('type' column in a polymorphic implementation), which
all have to be renamed. (So it's not quite as simply as dragging them into a
folder)
A foreign key to this model also usually then needs a class_name on it, except
I think in cases where the foreign key is coming from another model that is
also in the same namespace.
-Jason
> On Feb 24, 2017, at 11:48 AM, Allen Madsen <[email protected]> wrote:
>
> I believe everything under app/models is loaded by default, so you shouldn't
> need to add a subdirectory.
>
> Allen Madsen
> http://www.allenmadsen.com
>
> On Fri, Feb 24, 2017 at 10:49 AM, Maurizio De Santis
> <[email protected]> wrote:
> [From the issue I opened about it:
> https://github.com/rails/rails/issues/28152]
>
> I don't know if this is more a bug report or a feature request, or even an SO
> question anyway:
>
>
> I have a model Activity which has a lot of sti types, so a lot of related
> class files. I would like to organize them into app/models/activities, in
> order to separate them from the other models. I can actually accomplish it
> using config.eager_load_paths += %W(#{config.root}/app/models/activities)
> directive, but then in development mode the following exception is raised:
>
>
> ArgumentError: A copy of SubFolderModel has been removed from the module tree
> but is still active!
>
> I guess the reason is that app/models/subfolder conflicts with app/models
> autoload. Anyway, is there a way to organize models using different
> directories?
>
>
> Steps to reproduce
>
> • Create an app/models subfolder
> • Put some models inside
> • Add config.eager_load_paths +=
> %W(#{config.root}/app/models/subfolder) to config/application.rb
> • Start the application in development mode
> Expected behavior
>
>
> Everything works fine
>
>
> Actual behavior
>
>
> Sometimes the following exception is raised:
>
>
> ArgumentError: A copy of SubFolderModel has been removed from the module tree
> but is still active!
>
> System configuration
>
>
> Rails version: 5.0.1
>
> Ruby version: 2.4.0
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/rubyonrails-core.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/rubyonrails-core.
> For more options, visit https://groups.google.com/d/optout.
----
Jason Fleetwood-Boldt
[email protected]
http://www.jasonfleetwoodboldt.com/writing
If you'd like to reply by encrypted email you can find my public key on
jasonfleetwoodboldt.com (more about setting GPG: https://gpgtools.org)
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.