Viewing ActiveRecord model fields in Ruby on Rails - ruby-on-rails

I'm new to Ruby and I come from a C# background so apologies if this is very basic. I'm navigating around a large Rails project and looking at the model (ActiveRecord) classes. I want to be able to easily see all of the fields of a class so that I can see whether a particular class has the fields that I need. I can't find any way of doing this in the RubyMine IDE. Is there any easy way or am I misunderstanding the way that dynamic languages work? The only way that I have found so far is by looking at the underlying database tables.
TIA

You can look at schema.rb under the db folder.

Related

Collection of text files in place of rails database

I'm currently constructing a Rails site to edit a collection of files used for configuration of various services. The files are simple plain text files. The point of the site is to provide an easy interface for editing the files as well as validate changes for the less technically incline individuals who will be editing them.
I've looked around but I can't seem to find anything on using text files instead of a database. What I do find suggests that what I'm trying to do may not be correct for rails at all. The closest thing have is this question, but the answers are less than helpful.
Is there a correct way to create an MVC for text files and not use a database at all?
Yes. Rails provides MVC with activerecord being default for models. you can keep rails views and controllers (VC part) and write your own models (M part). Imagine using mongoid ORM instead of active record. For that reason rails provides activemodel to make it easy for people to write their own models that are backed by different storage mechanism. look at https://github.com/rails/rails/tree/master/activemodel to get started on writing your own ORM that will use text files as backend instead of SQL database. Also include http://api.rubyonrails.org/classes/ActiveModel/Lint/Tests.html in your model test files to verify that your models conform activemodel api. After that, you can use rails form helpers in views and routes with your models without problem.

Models in Rails are implicit, is this an annoying thing?

Considering models in Rails:
class Organization < ActiveRecord::Base
belongs_to :OrgType
end
Does it bother you that models in Rails don't include the fields that made the entity?
I know this is done for DRY's sake but you have to check the data base tables schema every time you want to check model's fields.
If you prefer a declarative style of ORM model, you might want to check out DataMapper or Sequel, both of which are easy to plug in to Rails 3.
Not annoying for me... ever since I started using the annotate-models gem to automatically add comments to the top of my model files, listing the fields associated with that model.
I was listening to a podcast a few months ago where the guy hosting the cast and the guest were advocating that Rails should take this path, and do away with migrations. The guy kept repeating "migrations must die", suggesting there be a way to specify your schema on the model instead.
I cannot disagree more, and I hope Rails never takes this path. Not only is it not DRY, but I like the fact that Rails encourages you to be aware of your own databases schema and structure.
In addition, there would be no way to keep a history of your schema if models were what controlled it, without having them be extremely cluttered. Migrations are essentially version control for your database that evolves with your application...and I wouldn't want to not have that.
Let's assume that OrgType has a field called position. This is common when you want to present a select list to the users who will be choosing a type. It's highly unlikely that your Organization will ever care about this field. Now extrapolate this to other related models with fields that other models don't care about, and add in the fact that if you ever wanted to change one of these fields you'd then have to hunt down each declaration of them, not just each place where they are used.
It's a mess.
Open up your db tool and look at your fields when you want to know what they are. Keep your models clean and readable. If you see code like this:
organization.org_type.name
It's pretty obvious that OrgType has a name field, without having to look it up, and without having to wade through configuration clutter in each model.

Rails functional testing without migrations

The name pretty much says it all. Does anyone know how to accomplish functional testing when you are not using migrations in Rails? I'd be open to any advice or third party libraries (if there are any). I thought of creating my own plugin to address this but it seems like a pretty big task and would rather not do this unless necessary.
Thanks in advance.
http://github.com/jpignata/temping
Temping allows you to create arbitrary ActiveRecord models backed by a temporary SQL table for use in tests.
It appears that there is no "easy" way to do this. I'm looking into RSpec rails to see if it can be modified to not use migrations.

How to create a user customizable database (like Zoho creator) in Rails?

I'm learning Rails, and the target of my experiments is to realize something similar to Zoho Creator, Flexlist or Mytaskhelper, i.e. an app where the user can create his own database schema and views. What's the best strategy to pursue this?
I saw something about the Entity-Attribute-Value (EAV) but I'm not sure whether it's the best strategy or if there is some support in Rails for it.
If there was any tutorial in Rails about a similar project it would be great.
Probably it's not the easiest star for learning a new language and framework, but it would be something I really plan to do since a long time.
Your best bet will be MongoDB. It is easy to learn (because the query language is JavaScript) and it provides a schema-less data store. I would create a document for each form that defines the structure of the form. Then, whenever a user submits the data, you can put the data into a generic structure and store it in a collection based on the name of the form. In MongoDB collections are like tables, but you can create them on the fly. You can also create indexes on the fly to speed searches.
The problem you are trying to solve is one of the primary use cases for document oriented databases which MongoDB is. There are several other document oriented databases out there, but in my opinion MongoDB has the best API at the moment.
Give the MongoDB Ruby tutorial a read and I am sure you will want to give it a try.
Do NOT use a relational database to do this. Creating tables on the fly will be miserable and is a security hazard, not just for your system, but for the data of your users as well. You can avoid creating tables on the fly by creating a complex schema that tracks the form structures and each field type would require its own table. Rails makes this less painful with polymorphic associations, but it definitely is not pretty.
I think it's not exactly what you want, but this http://github.com/LeonB/has_magic_columns_fork but apparently this does something similar and you may get some idea to get started.
Using a document store like mongodb or couchdb would be the best way forward, as they are schema-less.
It should be possible to generate database tables by sending DDL-statements directly to the server or by dynamical generating a migration. Then you can generate the corresponding ActiveRecord models using Class.new(ActiveRecord::Base) do ... end. In principle this should work, but it has to be done with some care. But this definitely no job for a beginner.
A second solution could be to use MongoMapper and MongoDB. My idea is to use a collection to store the rows of your table and since MongoDB is schema less you can simply add attributes.
Using EntryAttributeValue allows you to store any schema data in a set amount of tables, however the performance implications and maintenance issues this creates may very well not be worth it.
Alternately you could store your data in XML and generate an XML schema to validate against.
All "generic" solutions will have issues with foreign keys or other constraints, uless you do all of that validation in memory before storage.

Is there a good admin generator for Ruby on Rails?

My current project is in Rails. Coming from a Symfony (PHP) and Django (Python) background, they both have excellent admin generators. Seems like this is missing in Rails.
For those who aren't familiar with Symfony or Django, they both allow you to specify some metadata around your models to automatically (dynamically) generate an admin interface to do the common CRUD operations. You can create an entire Intranet with only a few commands or lines of code. They have a good appearance and are extensible enough for 99% of your admin needs.
I've looked for something similar for Rails, but all of the projects either have no activity or they died long ago. Is there anything to generate an intranet/admin site for a rails app other than scaffolding?
Active Admin (http://activeadmin.info/) was released in May of 2011, and looks like it's going to become the best Rails 3 option.
rails_admin appears to be the latest-n-greatest free project as of January 2011.
...best of all, there has been a lot of activity in the repository.
Scaffolding is the normal way to create an admin backend BUT there is a project called ActiveScaffold which may solve your problem.
Here is a roundup of a few options, including more than just ActiveScaffold.
ActiveScaffold is available for Rails 2.3.x :)
Just for someonse's info who have found this question one year later like me :)
ActiveScaffold is a good solution, but if you want a more configurable and powerful tool, I think Typus is a great solution:
http://github.com/fesplugas/typus
You have mainly two:
ActiveScaffolding: the most popular but be careful with rails 2.1
Streamlined
ActiveScaffold is by far and away the most configurable/easiest to integrate/most automagic scaffolding around at the moment.
It has built in ajax support, near seamless db introspection and it even plays nicely with legacy Oracle databases (which can be a real pain in Rails).
Try it: http://activescaffold.com/
Have a look at Casein (http://www.caseincms.com/), might be what you're looking for.
Having also tried typus, caseincms and ActiveScaffold over the weekend, I can't rave enough about admin_data.
It is
super-quick to install (Rails 3 is the gem, Rails 2.3 is a plugin branch,
no digging through trees on github),
unintrusive (all code is in the vendor/admin_data folder or the gem where it belongs),
requires no set-up and optional configuration is one block in one file in your app,
correctly (!) gets all model information from your model definitions (primary_key, foreign_key, relationships etc.),
including multiple databases, SQL Server connections via activerecord-sqlserver-adapter, and even composite primary keys, as everything is abstracted on top of ActiveRecord, if you model works, admin_data will work,
works great with legacy data for the above reasons,
uses your existing authentication solution which is called in the most wonderful DRYness in your configuration file.
It maybe less flexible or pretty than other solutions, but this plugin does many thingks right for quick admin panel setup.
The most common way to create a CRUD interface is to use Scaffold.
./script/generate scaffold_resource MyModel property:type property2:type2
This command would generate a CRUD interface for the model named MyModel (singular) with two properties. Properties is what's called columns in DB lingo. So you could have name:string age:integer active:boolean etc.
I can suggest you active_admin that is best
Active Admin main site

Resources