I want to generate documentation for my rails application. The application has models with relationship(e.g. has_many relation) and I am looking for a way to generate documentation that shows the relation between models. Ideally, with some graphs those represent the relationships between models.
Is there a way to achieve this in rails 3.2x ?
Check out the rails-erd gem. It generates entity-relationship diagrams from your models.
The Rubymine IDE also does it on the fly, but it's not free.
Related
I'm new in Ruby on Rails, I'm wondering, is there a user interface to see all the data that you have in your Active Record Models and manipulate them just like PhpMyAdmin?
Sadly no - you have to write that interface yourself... :)
There are ways to quickly scaffold up an interface for each of your models. Look at doco on rails generate scaffold for more info.
There are also gems that will automate some admin-scaffolding for you:
Look at eg ActiveAdmin
But in practice nobody keeps either of these around for long in their projects (for various reasons).
If you're new to Rails, I can highly recommend reading your way through the Rails Guides here: http://guides.rubyonrails.org It will help you level up in all the fundamental aspects of Rails :)
There are gems in rails to maintain tables in rails app:
ActiveAdmin: It provides admin panel using which you can add/update/delete any record under tables.
rails_db_info: Just and it under Gemfile and run bundle install. Go to "/rails/info/db" link, it will list all tables with schema details. Its very easy to integrate.
I wrote down on a simple text file the script I have to run to generate my models for my Rails 3.2.1 app:
rails generate model Champion name:string title:string story:string smallpicture:string largepicture:string spotlight:string
rails generate model Item code:int name:string description:string cost:string
rails generate model RecommendedItems mapname:string item1:int item2:int item3:int item4:int item5:int item6:int champion:references
rails generate model GoodAgainst reason:string champion:references
rails generate model BadAgainst reason:string champion:references
rails generate model Spell name:string description:string effect:string cost:string range:string champion:references
rails generate model Tip tiptext:string champion:references
rails generate model ChampionStat name:string value:string modifier:string champion:references
rails generate model User summonername:string email:string password:string confirmpassword:string location:string canvoteonfavoritechampion:boolean
rails generate model FavoriteStream url:string user:references
rails generate model ChampionRanking startweek:datetime endofweek:datetime champion:references
rails generate model CustomBuilds item1:int item2:int item3:int item4:int item5:int item6:int champion:references
Now, I realize that I can run generate scaffold, but I figured I would run model first and then add the scaffolding on a need only basis. Is this possible?
Is this the way you create a Ruby on Rails application? I drew out the database on paper and created the foreign key relationship and use the foo:references notation to note the relationships.
Is this correct?
Assuming that the syntax of your commands is correct, there's nothing objectively wrong with what you are doing, but I don't think it's the way Rails was meant to be used.
What your doing is very not agile. In agile programming, you focus on delivering working software early and often. So you would just make the minimal number of tables and columns to get the first few features working. Then once you have done that and shown it to your customers/users, you would decide what feature to add next and add the tables and columns you need for that. Eventually you would have a complete app.
I think this approach has some benefits because you might learn some things about database design during the early phases of the project that help improve your decisions in the later part of the project.
I recommend reading Agile Web Development with Rails if you want to see an example of how an app would be developed this way.
Another good practice is test-driven development. The idea is you should only write code to fix broken tests. It looks like you aren't doing that.
It is definitely one way to go about creating a Ruby on Rails application.
Depends on the developer, complexity of the application and how much of the design has been already completed. To get started you can load a schema directly from a existing database, and then create models for each of your relationships (my preference, as long as you know the Rails naming conventions). Relationships can get complex, so generating models is not always the most ideal option.
FYI: Ryan has a handy gem to generate scaffolding, layout files, authentication, and more. https://github.com/ryanb/nifty-generators
I'm new to Ruby on Rails, and I'm trying to create a bass guitar tutor in order to teach myself RoR (and bass guitar). The walkthroughs use Scaffold to create ActiveRecord classes, but they seem to correspond to standalone tables; there's no use of belongs_to or has_many.
I'd like to create three classes: Scale, GuitarString, and Fret. Each Scale has many GuitarStrings, which each have many Frets.
How do I create classes with this relationship using Scaffold? Is there a way to do it in one go, or do I need to create them in an unrelated state using Scaffold, then add the relations by hand? Or should I ditch Scaffold entirely?
I started learning Ruby on Rails a few weeks ago, and I've found it a lot easier to get the hang of things and learn my way around by not using scaffolding, and generating the various parts from the command line (or macros in an IDE).
However, from what I can tell, when you use scaffolding to generate things, you think of it as generating a "resource", so you're only going to create one resource at a time, then add in the relationships by hand later.
However, the generate model command can create these relationships for you. Lets say you used scaffolding to create a Scale resource.
You could then do
ruby script/generate model GuitarString name:string scale:references
The scale:references will create a belongs_to :scale on your GuitarString model, but you'll need to add has_many :guitarstrings to your scale model.
The generate model command also creates a migration script for you and other files needed (fixtures), similar to scaffolding, but doesn't autocreate views or controllers or anything.
EDIT:
This is generally how you are going to want to do things - use the generate/model or generate/view or generate/controller or generate/migration. Most Rails developers don't use scaffolding, since its "one size fits all" rarely fits things perfectly. However, most rails developers do use the generate commands I mentioned - it saves time with creating helpers and fixtures by hand, and it gives each file it generates a basic template you can add to.
Several Ruby IDE's like JetBrain's RubyMine have macros that essentially perform these commands. In RubyMine you can do ctrl+alt+g, then enter another key corresponding to what you want to generate.
The belongs_to relationship can be generated by using the "references" word, as I mentioned. Others you will add in by hand.
I am considering using MongoDB (mongo-mapper) for a portion of my rails application. I am not ready to go whole hog MongoDB because there are too many useful gems that depend on a traditional DB.
That being said there are parts of my application that would be great to leverage a document database.
Has anyone had success mixing the two approaches? How do you link activerecord models with mongomapper models?
MongoMapper doesn't implement ActiveModel yet, but I think there are a few forks on github that do. You could use Mongoid instead (which does) and your relationships between Mongoid docs and ActiveRecord entries would just magically work. I know a number of people are doing that.
That said, I wouldn't want to mix them unless I absolutely had to have an RDBMS for some reason.
Here a presentation about this issue: http://nosql.mypopescu.com/post/541657350/presentation-blending-nosql-and-sql-at-confoo
I don't know ROR so I can't judge it is a good presentation.
http://railscasts.com/episodes/194-mongodb-and-mongomapper
http://www.mongodb.org/display/DOCS/Object+Mappers+for+Ruby+and+MongoDB
http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails
http://www.mongodb.org/display/DOCS/Ruby+Language+Center
You need to mixin mongomapper with the model class
This gives you freedom to define the key-value pairs other than activerecord
include MongoMapper::Document
Dead simple I think.
What's the best way to create a model in Ruby on Rails that doesn't have an underlying implementation in as far as a database table goes? It's very common to write classes that perform behavior on a particular problem domain, yet can use some of the benefits that ActiveRecord has such as validation.
Is it best to just create it as a module or helper? What is the best practice here?
Checkout the screencast by Ryan Bates that covers exactly this - Tableless Models.
http://railscasts.com/episodes/193-tableless-model
With this approach, your Model would still subclass ActiveRecord::Base but define the columns manually, which allows you to use ActiveRecord features such as validations, and associations without a database.