I'm running rails v 3.2 .
After some time developing application I need to move some models into namespace ex: /app/models/address.rb to /app/models/local/address.rb
I tried simply moving models to destination folder and adding Local:: to the class name. then i accordingly update specs, and i still get an error on running spec.he . uninitialized constant Address (NameError).
The question is - How can i move models to a namespace? what are my actions - editing migrations or something else... Please help because i got mixed up by different articles & etc.
Update
I found out what's the problem:
Devise and namespaces. solving it
I think you can try this:
create a new rails project, then run
rails g model Local::Address city:string country:string
Now you can see how rails handle namespace for model. You can just follow the way to modify yours.
Related
In our existing Rails 3 applications we use an overridden migration.rb file to customise the table creation behaviour.
This was done in Rails 3 by placing our custom file in lib/templates/active_record/model/migration.rb, however it appears Rails 4 has changed the location used for these templates, and this override isn't picked up anymore (it uses the default ActiveRecord migration when creating tables).
I've had a look through the 4.1 ActiveRecord code but can't get the override to work again.
Does anyone know the correct location to place our custom migration.rb in a Rails 4 codebase?
EDIT FOR CLARIFICATION
When you create a new model in Rails, the migration that is generated for you is based off the template found in (> 4.1.x) activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb in the Rails gem.
This has changed since Rails 3.2. In Rails 3.2 the template that was used was called migration.rb and was in the activerecord/lib/rails/generators/active_record/model/templates directory of the Rails gem.
In order to customise the generated template (add a custom SQL block that would be executed in the change method), we override this file by placing a modified copy of it in in our local code base under the lib/templates/active_record/model directory.
We customise it to add some application specific SQL to the end of the migration. In rails 3 this meant that any time you generated a new model, the resulting migration would auto-magically include our custom SQL at the end of the migration.
In our Rails 4 upgrade this custom migration isn't being used anymore, so we're getting vanilla migration files generated by rails, and are having to manually add the SQL each time.
I have tried following the same convention and placing the file in lib/templates/active_record/migration/migration.rb (and a variety of other locations) but the custom template is not being used by Rails when generating a migration.
In Rails 4.2.6 those paths are like this:
lib/templates/active_record/migration/migration.rb
lib/templates/migration/templates/create_table_migration.rb
And the ultimate answer to this problem is to look into source code of Thor library, because all rails generators are based on it. This is how I found correct paths.
Go to lib/thor/actions.rb file and look for find_in_source_paths method and just add puts statement there. Whenever you run any generator you can see a list of all paths that are searched for templates. There are also other ways, but this should give you an idea.
In Rails 4.1.4 this should go here:
activerecord/lib/active_record/migration.rb
Source:
http://api.rubyonrails.org/files/activerecord/lib/active_record/migration_rb.html
I have used two different databases for my Rails application: MongoDB and MsSQL using Mongoid and activerecord-sqlserver-adapter adapter respectively. Everything is well but there is a problem while generate Model.
The problem is "how can I generate the model that relates to MongoDB or MsSQL differently?"
For example: I want to generate People model relates to MongoID and Animal model with MsSQL. While I generate with command: rails g model Animal name:string it generates the model related to mongoid. How can I generate the model Animal with ActiveRecord that means related to MsSQL.
Please help me.
Thanks
Based on Using Active Record generators after Mongoid installation? I believe this should work:
rails g active_record:model Animal name:string
First let me just check that I've understood your question correctly:
You have 2 databases and a series of models/migrations, and you want a way to tell rails which database to use when running a migration and accessing the database using your model?
If I'm in the right area then you need to add a method to your migration which overrides the default connection() method in ActiveRecord::Migration.
def connection
ActiveRecord::Base.establish_connection(:conn_name).connection
end
Where :conn_name is the name you gave your connection settings in config/database.yml
within your models add the line
establish_connection :conn_name
to the top of your model file and the model will now know which DB to connect to.
So the quick and dirty way that I have handled this in the past (due to my dev team keeping mongoid in the gem file for legacy reasons) is to comment out the out mongoid when you have to do migrations run a bundle, generate and run you migration then uncomment and run bundle again. This is far from best practices but it should work.
On my blog I'm have posts that belong to a series. I've tried to scaffold series but there are some problems with routes.
The pluralization engine doesn't get it right so I had to manually change Sery, #series, and #sery which is not a big deal.
The routing seems to be ok with resources :series. But then when I try to create a series the form_for helper complains about the route.
And then when I create it with console it works but rails is still complaining about routes.
Please create a simple app and see what the problem is.
rails new test_series_app
And then run the scaffold generator:
rails g scaffold series name:string
And see how the routes are getting mixed up and help me out please!
For the record, I put the singularize code into the scaffold generator (yes, my one contribution to Rails). All it does is check if record_name == record_name.pluralize. If it does and you haven't passed in --force-plural, it calls record_name = record_name.singularize.
In this case "series".pluralize is the same as "series".singularize so I assume it won't do anything.
So if you're having problems w/ it, you need to write an inflector for the word.
(I wrote it after Jeremy Kemper's 2008 RailsConf keynote in which he accidentally passed in a plural model name causing himself all sorts of grief in the middle of his talk.)
Say I have the model CourseGroup. What would be the controller's name?
The controller name would be course_groups_controller.
http://itsignals.cascadia.com.au/?p=7
To find the name for any model, you can open up a rails console and do "ModelName".tableize. Then just add "_controller" to the end. This would result in model_names_controller.
Here's an easy way to find out the naming conventions: Just create a throw-away Rails app in a temp directory, with a scaffolded model:
rails blog
cd blog
./script/generate scaffold post subject:string content:text
You can then browse through the files and directories to see how things are named. I like to keep one of these around just to refer to from time to time. And by the way, running the generators without any parameters gives help output which includes examples of naming conventions:
./script/generate scaffold
NameError in GenresController#index
uninitialized constant GenresController
RAILS_ROOT: C:/Users/Will/Desktop/INSTAN~1/rails_apps/talewiki
I've created a table called Genres and when I try to connect to it via local host I get the above error.
Any ideas?
With all the questions you're asking I believe you're an absolute beginner regarding ROR. Perhaps you should visit some tutorials to learn rails.
I don't know what your genre model describes, but I think it will have a name.
Basic steps for a basic genre model:
Delete the table for your genres if created manually (with SQL code)
DROP TABLE genres;
generate a complete scaffolding for genres:
$ ruby script/generate genre name:string
$ rake db:migrate
Now you have a complete controller for all CRUD actions for a simple genre model
If I were you I would read some tutorial about RoR, because you make the impression that you don't understand RoR or the MVC principle behind it. A good start would be: http://storecrowd.com/blog/top-50-ruby-on-rails-tutorials/
You need to generate a controller to handle the index action when your browse your application on localhost
ruby script/generate controller genres index
run that from your console within your application and it will generate the GenresController with the action index (it will be an empty action but you shouldn't see an error when browsing http://localhost:3000/genres/)
file
C:/Users/Will/Desktop/INSTAN~1/rails_apps/talewiki/app/controllers/genres_controller.rb
must be present