Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model inside a subfolder within models folder

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.

like image 763
Brian Roisentul Avatar asked Jul 02 '10 21:07

Brian Roisentul


Video Answer


2 Answers

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.

like image 100
Geoff Lanotte Avatar answered Nov 08 '22 20:11

Geoff Lanotte


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

like image 31
Zepplock Avatar answered Nov 08 '22 18:11

Zepplock