rails generate model(s) through configuration file - ruby-on-rails

There are possibilities to generate models (and migration) through a configuration file? I have so much table and i want create them (all the time) with a simple step.

I'm holding all my migration commands in a bash shell file. This allows me to "recycle" certain parts, and prevents me from making typo's... Once a command has been run, I outcomment it, but keep it present in the file

Related

How to initialize empty tables based on my rails schema without initializing in Rails?

I'm tasked to create a setup documentation from a project that will be passed on to new developers -- which needs them to setup the said project in their own systems.
Apparently, upon rake db:schema:load I found out that the project has model dependencies from files under /config/initializers which loads first before the models do.
I'm thinking that creating a script that would initialize empty tables first would be a good solution. Though I'm not sure how to do this without manually listing all the databases currently in use -- what if there is an additional table added in the future? This means I need to update the script everytime. Any way I can read the schema dynamically without actually running it (because, some models also need some things to be loaded from the config/initializers first.
Thanks!

procfile got created twice in rails

I created a Procfile for my rails app. When I commit the change to github I can see that 2 Procfiles are there. One is the one I created Procfile and the other Procfile~(This one was hidden). I can see that the contents are same when I check on remote repository. Is it normal or should I delete one?
You just need one Procfile. Usually a file finished with a tilde ~ is a backup copy made by your editor.
The systems relying on the Procfile will just look at the one with that name, they won't care about Procfile~, so it's up to you if you want to delete it or not.
A common thing to do is to add the following in your .gitignore:
*~
On many unix based systems, a tilde is added to a filename when a temporary save is made in order to avoid data loss and concurrent editings of a single file. If the contents are the same, you can probably delete Procfile~.

Save and Run Rails Script

Is it possible to write a set of commands - similar to what you'd find in a controller - but save it as a standalone file in my rails app that I could call up and run from the console?
For instance, something to loop through my database and run some ActiveRecord commands to modify data that may need to be cleaned up from time to time, but I don't want to live inside a controller. It's large - too large for me to copy/paste it into the rails console - which is why I'm trying to make a file out of it.
You can write your code into a separate file, let's say myCode.rb and run it from the console with rails runner myCode.rb (run this command from the directory you saved the file to). rails runner gives you access to your rails environment.
Hope this helps.
I took your advice, Tony, and looked into making my own rake tasks and that seemed to do the trick!
I made a new .rake file inside my lib/tasks folder and just copied and pasted my block of code inside there. I had to set it to inherit from the environment, as stated in the post I read, but it seems to work fine now - thanks for the tip!

Ruby on rails without using scaffold and generator?

I am new user to ruby on rails.
I have some question please give the answer as early as possible
1) Is it possible to create web application without using *rails new application_name* command? means creating required folder and file manually?
2) I want to create application without using scaffold and generator, so everything is created manually...I searched but not get a link to do it...
You really should be using rails new (appname) to generate your project directory.
From there, you do not need to generate a scaffold. If you want to go slightly less abstract and create some things manually you can use rails generate resource (resource name).
If you want to go even less abstract then that, you can use rails generate model (model name) and rails generate controller (controller name) and rails generate migration (migration name). Within this level of abstraction, you can specify options such as methods you want the model to have or columns you want the migration to add.
And the least abstract(most manual) would be if you make these files yourself (like actually going in and creating new folders/files for models, controllers, etc.)
So on an order from most abstract to least:
1) generate scaffold
2) generate resource
3) generate model/controller/migration
4) creating the files/folders without rails
Most developers are usually working with the #2, #3, or #4 layers (remember it is always a tradeoff between eliminating a lot of time off by not having to manually create the same code over and over again and flexibility).
rails new app-name creates tons of files and folders which are necessary for the app to run. You would waste tons of time writing them yourself. Read as "reinventing the wheel".
You can simply create a controller and view file. Add the corresponding route. Start the server. Voila. You will have it. Most intro-articles showcase Scaffold to show the power of Rails i.e how much can be achieved with few lines of code.

Should I use migrations to create folders?

I created a feature that requires two folders to work correctly. These folders didn't existed in the rails project so I had to create them. Now I need to push this functionality so that the other developers can use it as well.
What do you think is the best practice:
Create the folders locally and leave a note somewhere explaining that these folders needs to be created
Create a migration that will create these folders
Create the folders if they don't exist on execution
So what are the best practices regarding this?
Two choices
1. Put them under version control.
When the developers next check out the folders will get created. How you add them to VC depends on the nature of the tool you're using but many tools require that the directories contain a file (possibly hidden).
That works great if the folders are in the source tree. You also have the potential on a Unix-like os to soft-link from within the source tree to a well known location outside of the source tree. Depending on the VC system (we use Mercurial), you can make the soft link a versioned item.
2. Create them as your process starts
If you're going to do this, you might as well go the extra mile and make their location a configurable option. On start up, read the config file, make the folders and continue.
I would think that as part of the dev builds there must be rake tasks that get invoked to do certain activities; it would be appropriate to either include such activity in a rake tasks where it fits most appropriately.
It should check for folders and if it doesn't exists; just create them. Migrations for creating folder is really not the right way I guess.
I'd include this in a bootstrap rake task.
This is a good question.
For me I would put the folder creation in the migration if and only if the folder was required as part of that migration. So for instance you were adding or updating the table dealing with attachments (such as with attachment_fu etc), then I would consider putting the directory creation/deletion in there.
However creating directories is an environment specific thing (different on Windows vs *nix etc), especially where permissions are involved. Therefore it would make sense to go where these issues are handled in an environment-abstracted way. Perhaps in the deployment script (if using Capistrano) or as other people have suggested in a rake task.
I'll add them to the repository. Maybe if it holds temporary data I'll ignore the contents.

Resources