Organizing rails model files into subdirectories -
i'm organizing models subdirectories listed below. i've tried every solution listed in stackoverflow few others keep getting errors. seems simple task , in need of help.
makandra - organizing large rails apps
rails 4: organize rails models in sub path without namespacing models?
how organize rails models fat?
rails models in subfolders , relationships
however, keep getting error:
activerecord::statementinvalid: not find table 'blog_posts'
config/application.rb:
1 require file.expand_path('../boot', __file__) 2 3 require 'rails/all' 4 5 # require gems listed in gemfile, including gems 6 # you've limited :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 on specified here. 12 # application configuration should go files in config/initializers 13 # -- .rb files in directory automatically loaded. 14 15 # set time.zone default specified zone , make active record auto-convert zone. 16 # run "rake -d time" list of tasks finding time zone names. default utc. 17 # config.time_zone = 'central time (us & canada)' 18 19 # default locale :en , translations config/locales/*.rb,yml 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 # 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 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
the rails convention table name model named blog::post
blog_posts
. can override setting .table_name
on model this:
class blog::post < activerecord::base self.table_name = "posts" end
anyway, i'd suggest follow rails conventions when possible. there reason not name table blog_posts
?
Comments
Post a Comment