views:

29

answers:

2

I'm using Ruby on Rails 2.3.8 and I'd like to know how to organize models in subfolders so Ruby can recognize them.

+2  A: 

Rails will load all models in /app/models recursively from subdirectories (for Ruby 2.0+). You can split them into logical subdirectories: database, users, customers, etc

Zepplock
+1  A: 

To the best of my understanding, you need to namespace your models in order for them to be loaded.

to use the generator:

> ./script/generate model Customer::Address

will create the model in app/models/customer/address.rb

class Customer::Address

end

It will load recursively, but in order for rails to find it, it will need to have the namespace that lines up with the path.

Fair warning that when you use the generator (at least in rails 2.3.5 and lower is all I have tested this in). It will create table name as customer_addresses, but the model will by default still look for a table name of addresses. You will either need to change the migration database name to addresses or add set_table_name 'customers_addresses' or similar to get the two to line up.

Geoff Lanotte