Rails: Active Model explanation? - ruby-on-rails

What's the difference from Active Resource and Active Record in Rails?

ActiveRecord: ORM to manage data in a relational database through SQL. read more..
ActiveResource: Manage data through a REST API on another server, no longer a part of Rails, but extracted as a gem. read more..
ActiveModel: Behavior (mixins) to make any Ruby object act like it's ActiveRecord or ActiveResource by adding validations, callbacks, naming conventions, tracking, etc. read more..

ActiveRecord acts as a layer between a specific database and your application. It is a conventional Object Relationship Mapper (ORM).
ActiveResource acts as a layer between a more abstract network resource such as a REST server and your application. It acts as a sort of wrapper to make a REST resource appear as a database model so similar techniques can be used.

Related

Any Active Record like gems can do the operations in Ruby object without accessing database

I need to do lots of queries for rendering a page.
For example, Flight.where(~~~~) that kind of queries is about thousands on rendering a page. However, the involved data is small, only thousands of objects.
I think If I can make those requests on memory instead of the accessing database.
It will be much faster.
Is there any gem to mock Active Record search syntax for Rails?
Therefore, I won't have to write a bunch of if-else, loop logic to filter the items.
Currently, I'm using Mongoid for accessing MongoDB on my Rails app
Thank you.
You should use ActiveHash gem as base class instead of ActiveRecord.
ActiveHash is a simple base class that allows you to use a ruby hash as a readonly datasource for an ActiveRecord-like model.
More details here.

Neography and Rails

I am experimenting using Neography with Rails 3 and can't quite understand where to specify DB connections, Model Validations (validates_presence_of) etc. The examples available for Neography does not have one for Rails. Would appreciate any pointers.
I don't think Neography integrates that closely with rails, or has an ActiveRecord mapper. You can find configuration information on their wiki: they don't explicitly say it, but you should copy that config in to initializers/neography.rb.
In regards to Models, specifically in regards to the Neography gem, you're probably best off storing data in normal Postgres/Mysql/sqlite, and using after_create, after_update, after_delete hooks in your models to then keep the neo4j database in sync. If you do it this way, you really only have to store the minimum amount of data needed in Neo4j (e.g. object ids and only the data your actively using for node and vertice graph operations). Then, when you query neo4j, you'd take the resulting object id's and "rehydrate" them by querying your SQL ActiveRecord models
The alternative is to use the Neo4j gem itself which offers ActiveRecord mappers and a much cleaner api. But I assume that you're not doing that because of environment constraints (for example, you want to run on Heroku and use the Neo4j addon)

Can we mix Mongodb dynamic attributes to an ActiveRecord model?

We are using a dynamic attributes plugin similar to this:
http://codaset.com/joelmoss/dynamic-attributes
Which allows us to store dynamic attributes in our rails model. Those dynamic attributes are in a single database column. We are facing performance issues because of this and I am wondering if MongoId, MongoMapper or other rails plugins will allow us to keep some attribute in ActiveRecord (keeping the < ActiveRecord::Base) but store the dynamic attributes in mongodb. We want to do this because we need to keep using MySQL for most of our existing system, but use MongoDB to store dynamic attributes about some models.
Basically this is what I am talking about:
http://www.railsinside.com/plugins/242-quickly-add-couchdb-to-existing-rails-models-with-stuffing.html
This plugin does the exact same thing using CouchDB.
I don't see any reason that MongoDB can't handle the same thing.
MongoMapper should implement the ActiveRecord pattern. However, Mongo also has some simple drivers for Ruby. All in all, the concept of storing dynamic attributes is a perfect use-case for MongoDB.
If you don't mind skipping "ActiveRecord" you can probably cook up your own using the basic Mongo drivers with very little work.
If you can't figure out the steps to do this, I would suggest pinging the groups (http://groups.google.com/mongodb-user/) and asking Kyle Banker directly (he's their ruby expert).

Overwrite ActiveRecord's save method to use custom interface

I got the following situation:
I have a rails application and two databases, one is for the Rails Application and the second database (running on the same DB Server Instance) is for a different non-web application.
My Rails Web Application may read from the (second) Database directly via SQL but it can't write to it because of security restrictions (the data has to be validated by the non-web application). So we wrote a little CLI interface based on SOAP for writing into the database.
My question is: Can I extend the ActiveRecord Model (Rails 3) in a way so that the reading goes as normal over the SQL connection but update/create/delete goes over our selfmade interface.
I think I found a good solution :)
# rtacconi: Thanks for your links but since Rails 3 you don't have to use these extensions because ActiveModel works table-less out of the box :)
I need full ActiveRecord support for reading the table but writing is done over my SOAP interface. This is because I can't validate the data in my Rails application.
So my solution is to overload the ActiveRecord::Persistence Module (can be found in activerecord-3.0.0/lib/active_record/persistence.rb.
This module is responsible for any write tasks to the DB connection. So instead of writing to the DB, my Persistence Layer calls the SOAP interface.
Best regards
Simon
You could implement a tableless model: ActiveRecord::Base Without Table or http://github.com/AnthonyCaliendo/acts_without_database
Than you can set/get data into an object using the SOAP library.

How to mix mongodb and a traditional db in Rails?

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.

Resources