3

I'm organizing my models into subdirectories as listed below. I've tried every solution listed in StackOverflow as well as a few others keep getting errors. Seems like a simple task and am in need of help.

Makandra - Organizing Large Rails apps

Rails 4: organize rails models in sub path without namespacing models?

How to organize Rails models that are too fat?

Rails models in subfolders and relationships

However, I keep getting this error:

ActiveRecord::StatementInvalid: Could not find table 'blog_posts'

config/application.rb:

  1 require File.expand_path('../boot', __FILE__)
  2 
  3 require 'rails/all'
  4 
  5 # Require the gems listed in Gemfile, including any gems
  6 # you've limited to :test, :development, or :production.
  7 Bundler.require(*Rails.groups)
  8 
  9 module Multifile
 10   class Application < Rails::Application
 11     # Settings in config/environments/* take precedence over those specified here.
 12     # Application configuration should go into files in config/initializers
 13     # -- all .rb files in that directory are automatically loaded.
 14 
 15     # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
 16     # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
 17     # config.time_zone = 'Central Time (US & Canada)'
 18 
 19     # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
 20     # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
 21     # config.i18n.default_locale = :de
 22 
 23     # Do not swallow errors in after_commit/after_rollback callbacks.
 24     config.active_record.raise_in_transactional_callbacks = true
 25   end
 26 end

Files:

app/models
├── blog
│   └── post.rb
├── blog.rb
└── concerns

Routes:

 3   resources :blogs do
 4     resources :posts
 5   end

models/blog/post.rb

 13 class Blog::Post < ActiveRecord::Base 
 14   belongs_to :blog
 15 end

models/blog.rb

 11 class Blog < ActiveRecord::Base
 12   has_many :posts
 13 end
Community
  • 1
  • 1
eternal44
  • 85
  • 2
  • 10

1 Answers1

2

The Rails convention for the table name for a model named Blog::Post is blog_posts. You can override that by setting .table_name on your model like this:

class Blog::Post < ActiveRecord::Base 
  self.table_name = "posts"
end

Anyway, I'd suggest to follow Rails conventions when possible. Is there a reason not to name your table blog_posts?

dgilperez
  • 10,716
  • 8
  • 68
  • 96
  • Thank you! I'm just trying to keep my models organized and ready to scale. It'll be easier to decouple or move around my associations if my tables are named separately. I'm open to alternatives though. Any suggestions for organizing larger apps? – eternal44 Aug 16 '15 at 23:15
  • It's a really open question, the answer depends on many constraints probably unknown yet. For larger apps you may find best to split it into different apps that exchange data through APIs. I'd suggest to stay with Rails conventions - as a safety measure to stay away for further pain down the road - and adapt the application as soon as requirements appear, not before. In this case, I'd just call the model `Post`. If you have many `Post` models, then I'd call it `BlogPost` and name the table `blog_posts`. If you prefer the namespaced format for the model, I'd still go for `blog_posts`. My 2 cts. – dgilperez Aug 16 '15 at 23:26
  • You're right, it already started causing problems: I can't generate new posts with that method. – eternal44 Aug 17 '15 at 02:16
  • hahaha :) Well, you surely may find the way around that as well. But it's just not worth it. – dgilperez Aug 17 '15 at 02:18