Use DataMapper instead of ActiveRecord [closed] - ruby-on-rails

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
DataMapper idea is definitely better than ActiveRecord. It has one API for a variety of data stores, including RDBMS and NoSQL stores.
DataMapper is much smarter then ActiveRecord. It has "Strategic Eager Loading". This feature single-handedly wipes out the "N+1 Query Problem". Additionally it allows lazy-loading of heavy fields like Text property. DataMapper allows you to create and search for any complex object graph simply by providing a nested hash of conditions. ActiveRecods is unsuitable to be used for associations in production. include method is ugly and unconfigurable. In October 2010 Josh Symonds makes a patch for this method to allow fields to be excluded from eager loaded. But that path was ignored and today in rails3 we have same ugly include method.
Even in Rails3 with brilliant AREL, ActiveRecord is far from the best orm for rails. You may say "wow, that new gem metawhere allows to write nice code like Article.where(:title.matches => 'Hello%', :created_at.gt => 3.days.ago)". But wait... DataMapper supports this out of a box! Maybe instead of modifying ActiveRecord to looks like DataMapper better is to extend and support better orm? More details of DataMapper can be found at http://datamapper.org/.
I think, that rails in later releases should allow us to select which orm to use, like now it allows a database selection. Even it will has only one option "ActiveRecord", people will search for available alternatives. When I was starting to learn rails, I thought that there is support only for ActiveRecord. And later I did not even try to look for something else.
Why I am writing all this stuff? I think, that we need to pay more attention to this nice ORM. If you are developer of some popular or not so popular gem, think about adding support of DataMapper. DataMapper community should write some migration guides from ActiveRecord or another ORM and keep documentation uptodate and your may help them. As for me, DataMapper community needs more people and you may be one of them.
The only disadvantage of this ORM is lack of documentation and you may help.
So what do you think about that?

Rails 3 allows you to use Datamapper, just not by default. It you do want to use datamapper with rails 3. Just do this
rails new project_name -m http://datamapper.org/templates/rails.rb
Rails allows you to select, you can even use the --skip-active-record option. :) ( May not be straightforward but its there.
And yeah, Datamapper is awesome. :)

The lack of documentation is a huge issue. I've begun my databasing using DataMapper and have since decided to implement a web interface for my data. Incorporating DM into Rails, as a beginner in each, has been a tremendous challenge. Hence, I'm curious, do you know of any resources which show DM/Rails usage? I'd love to be able to see something when I'm not sure how to proceed.

Related

Is Rails Enterprise Friendly? ActiveRecord seems selfish [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have been learning Rails since this summer and have written some successful standalone apps with it, and really have grown to appreciate nearly everything about Rails.
However, my current project is writing Rails as a front-end to a database that will be used for other things (web services, data warehouses, other apps connecting) -- and I am realizing that ActiveRecord, and largely all the Rails developed around AR, seem to be very one directional when it comes to this approach.
Note: I am not referring to a Legacy database, ie. one that we have to plug into, but was designed "back in the day"..
Rails seems to think it is, and should be, the only application working with this data. I am noticing it more and more when it comes to things like, composite primary keys (which is something you cannot just adjust, like table naming and column naming).
While I know there is a composite key gem, I feel like I am developing an entire MVC application dependent on this single gem working. I fear I will run into more and more things like that. I have looked into alternative ORM's, however it seems that many gems depend on ActiveRecord.
I guess I am a bit surprised and saddened at this apparently selfish angle that ActiveRecord has.
Anyone have any feedback or success about how Rails works with an enterprise level database?
Rails is built primarily as an "opinionated" web development framework.
What it is built to do out-of-the-box is full-stack web applications, where the entire system is built the "Rails way", which is the result of many decisions made by committers to Rails to keep the system simple and standard. The number of configuration decisions that need to be made are very small when you stick to the conventions.
These conventions include setting up your databases in a certain way. Primary keys are auto-increment and named 'id', foreign keys are 'foreigntable_id'. ActiveRecord is built-in and almost always assumed to be used (even when it isn't).
None of this means that straying from the standard stack won't work. Sequel and DataMapper work just fine. You can name your table columns (nearly) anything you want. If your application can do what it is supposed to do; that is the most important thing.
Yes, because you used Datamapper, now your app depends on Datamapper. How is that a bad thing?
Understand that because ActiveRecord is the default, that many other gems build on ActiveRecord to do their thing, so they may not work with Datamapper. That is not Datamapper's fault (or Rails').
The real question here, is "Is Rails, even when you use an alternative ORM or the composite keys gem, still more suitable and productive than X?"
I would argue that it is. So what that I have to set self.table_name= and self.primary_key= in every class? So what if I have to be forgo some gems that only work with ActiveRecord? So what if you have to consider database triggers and views. I deal with all these things, and Rails is still a joy to work with compared to everything else I've ever used.

Should I move to MongoDB to avoid SQL injection vulnerability in Rails? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
There is a big concern regarding a recently-disclosed Rails SQL injection vulnerability. I am not well-versed in dealing with security, so I am unnerved. I hear that MongoDB is not susceptible to this kind of attack.
Is this a good reason to move to MongoDB permanently?
No. Just because SQL databases are vulnerable to SQL injection attacks is not a good reason to move to MongoDB.
MongoDB is great in certain use-cases, particularly where you need schema-less collections.
If you need to store relational data, you'll be much better served by a relational DB.
In short, if you're afraid of SQL injection, learn how to sanitize your inputs. Don't move to MongoDB.
The newly found vulnerability does not affect everyone. Read here for more details.
SQL != NoSQL
A relational database using SQL is not a feature-by-feature replacement for NoSQL. Depending on your problem domain, you can sometimes make different architectural or design decisions that favor one over the other, but you can't simply swap them out like spare parts because they have very different design goals and performance characteristics.
All Software Has Risks
Software security is a big topic, and well outside the scope of an SO question. However, switching from a product that is widely used and quickly patched to a product that you don't understand is not inherently a good risk management trade-off.
In point of fact, the vulnerability you're referring to is a Rails vulnerability that has since been patched; you have zero guarantees that there won't be other Ruby or Rails vulnerabilities in the future---or, in fact, network or OS vulnerabilities---that target different parts of the MVC or OS stack. Therefore, I'm not sure what you think you'd gain from a risk-management perspective by painting SQL as the inherent villain in this story.
SQL injections are a problem which is known for decades and well-understood. There are coding practices which make SQL injections completely impossible when you adhere to them:
Run all variables through escape functions before putting them into a query
Use prepared statements
Use stored procedures
Always use at least one of these practices, and you don't have to be afraid of SQL injections.
MongoDB, on the other hand, is a rather new technology. It is not known yet what kind of newbie mistakes will be the most typical which lead to vulnerable applications. Maybe there is something just as bad as SQL injections everybody is doing right now without realizing. Maybe the exploit is already circulating among the black hat hackers. Who knows?
Also, as CodeGnome pointed out, MongoDB is not a drop-in replacement for an SQL database. It has a completely different philosophy. 83% of all MongoDB questions around here are asked by people who try to use MongoDB as if it were a relational database and wonder why it doesn't work out.

Ruby on Rails based website and app [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have started coding during my free time for over a year now. I had a social community idea which will be location based which basically deals with locations and nearby trending places.
I did some 10 minute video about scaffolding and have been totally smitten by Ruby on Rails. I really want to go ahead and make it on RoR.
However I don't know how to work with databases dealing with locations. I have done some research and looks like Mongo.DB is the one which is the most popular considering what 4square did. But I was wondering can we work with Active Record ? I saw a gem called geocoder , will it help me with the problem I am trying to tackle ?
Again , I have only coded in C++ , and haven't exactly worked with databases .
Which gems and tools should I be using for it ? Ruby and Mongo.db or Ruby on Rails simple using gems like geocoder ?
I felt encouraged seeing a website called www.foodspotting.com , it shows they have coded in ruby on rails ! Can someone give some good combination so that I can start thinking in the right direction ?
ruby on rails with mongo?
ruby with sinatra ?
although I am trying to stick to the RoR genre , as I have subscribed to the Michael Hartl tutorials .
I'd suggest using PostgreSQL with PostGIS. You'll need the activerecord-postgis-adapter gem to make that work with ActiveRecord.
However, a word of warning, dealing with spatial data isn't easy, and if this is your first Ruby on Rails application and your first database-backed application I would say that you're biting off a fair bit more than you can be expected to chew. Start small, start easy, work your way up.
First try to make some small applications in ruby on rails using database like MongoDB. I suggested this because you are going to use location based application implementation.
A very nice tutorial step by step that will let you go for it is -
http://www.mongodb.org/display/DOCS/Rails+3+-+Getting+Started
Regarding Geocoder - It provides object geocoding (by street or IP address), reverse geocoding (coordinates to street address), distance queries for ActiveRecord and Mongoid, result caching, and more. Designed for Rails but works with Sinatra and other Rack frameworks too.
You will find the documentation of geocoder to let you start over here -
http://rubydoc.info/gems/geocoder/1.1.5/frames
It is not a matter of choosing between MongoDB and PostgreSQL or between RoR and Sinatra.
What you really need is a RoR plugin/engine/module/whatever that can help you with map viewing, navigation, locating and geocoding. The geocoder gem is a component of this software stack.
The geocoding/mapping/navigation toolset you will choose will impose you the DB (most likely PostgreSQL with GIS extensions, that is: PostGIS) and many others implementation details.
A very well-known and largely appreciated mapping/navigation software stack is the one developed and used by OpenStreetMap (http://wiki.openstreetmap.org/wiki/Main_Page) that (you are a lucky guy...) is even based on RoR.
Have a look at their solutions and have a talk with the developers on their forum/mailing-list. I'm sure you will find what you need.

Propel or Doctrine; which is better ORM for Symfony 1.4 [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I've live project experience with CodeIgniter and Cake PHP. However now I need to work on a huge project for which both are unsuitable. I'm nearly decided to go with symfony 1.4 (2.0 isn't an option, according to my client's requirements).
In Symfony 1.4 too, I'm confused between ORM choice: Doctrine or Propel? I went through few links. Best found so far is
PHP ORMs: Doctrine vs. Propel.
However all those comparisons seem a minimum of two years old and definitely the world would have changed a lot since than. I do not have any issue with coding style; I'm fine with all Active Record, Criteria, DQL or whatever. For me, performance is most important than coding style comes at last. I'm mainly concerned about performance while dealing lot of data, probably millions of rows from multiple tables under clustered database. Unfortunately right now my experience is not sufficient to take independent decision on that matter.
Can anyone please shed some light on performance of Propel/Doctrine under Symfony 1.4? Other than performance, is there any other notable factor (except coding style) which one should take care while choosing PHP ORM?
tl;dr: I used Doctrine for a long time and I will choose Propel if I have to start something new.
This is such a common question that has nearly no good answer. But I will just give you my point of view.
I've worked with Doctrine since the first alpha (and with the old symfony 0.63). We choose Doctrine instead of Propel because Doctrine supported PDO (which is native to PHP) and Propel was still running on Creole (which isn't native). Creole was very slow in comparison to PDO (of course).
Recently, Doctrine added lost of a magic. I mean, you could call getField, findOneByField for everything and it would return what you want. It was something really great instead of having to build your own getter and setter. Magic was really trendy at this time.
Writing query using Doctrine was really easy instead of the painful Criteria and Criterion from Propel, which was really verbose. I was really fan of Doctrine and recommended to everyone to start using it instead of Propel.
Then, Propel switch to PDO as of 1.3 and started to have a nice API to write query, almost the same approach as Doctrine. The main difference was that Propel generate all magic things while Doctrine build on the fly. That's the biggest difference I think.
Propel doesn't have any magic in this code. It generates all getter/setter, join, etc .. when you build your model. Doctrine does every thing when it run the query. That's okay for small to medium project, but it starts to be bigger, it will become a slow solution. And it's great for debugging too, because you find the code in generated classes, you don't have to jump from classes to classes to find the global method that handle this case.
Both ORM use behaviors. I love behaviors. They are handled in different way in Doctrine and Propel. Doctrine still use its magic to handle them where Propel generates everything from the behavior inside the class (one more point from generated class instead of magic one).
As of now, nothing has really changed in Doctrine (to give a shot from the thread you mention) because of Doctrine 2 (which isn't native at all for sf 1.4) since the 1.2.x branch is almost dead (last release are 24/08/2010). Propel is still active, well really active, as you can see on github.
I still work with Doctrine but I've learn a lot from Propel since few years. I've build some personal projects on Doctrine. As of today, I changed my mind, and if I have to start a new project I will do it using Propel.
Few links:
Should I choose Doctrine 2 or Propel 1.5/1.6, and why? (on programmers.se)
The POV from the Propel lead dev on this choice (in favor to Propel, of course)

Rails + Oracle: worth hassle? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Is it architecturally sound to use Rails and Oracle?
My concern is that Oracle would be too "heavy and hi-end" for Rails.
Any thoughts?
Thanks.
The database needs of Rails more or less boil down to tables, indices and CRUD.
Anything that provides those ought to be able to serve as the back-end to a Rails app, and Oracle certainly does those parts somewhere among the bloat vast range of features it provides. If you can use ActiveRecord migrations then you shouldn't need to worry too much about platform-specific aspects anyway.
The oracle-adapter for ActiveRecord (which needs to be downloaded separately and placed in activerecord\lib\active_record\connection_adapters, btw) makes use of Oracle-specific elements where appropriate, the main one I can think of being the use of sequences for id generation.
The Oracle query optimiser is very smart, probably rather better for complex multi-table queries than, say, MySQL.
If you're looking at Oracle as a back-end for an externally-hosted site, then I'd be sceptical on cost grounds, as well as the reservation already stated about availability. But I suspect that the question relates more to what's already available in-house, in which case I'd say it's about as "good" a solution as you can get.
I'm developing intranet applications on Rails/Oracle, using OracleXE locally for development, deploying to some "big iron" machine (it's someone else's job to care for it, I don't even know what OS it's running) for production.
EDIT: It's worth looking at the activerecord-oracle_enhanced-adapter (it's a gem) as a replacement for the built-in oracle adapter, if only for the improved handling of date/datetime values (Oracle only has one column type for both: DATE). It's probably saved me from a breakdown...
Why too heavy? Oracle it's just a database that can be simple to heavy depending on your use. Today Oracle have a lot of editions to accomodate any use, for instance the little one Oracle XE.
If your are happy with Oracle, use it. Don't worry.
The problem that you may suffer when using Oracle as Database is you can't find many hosting companies offering oracle as choice for your database on web.
also oracle like a giant DB, if you don't need the specific features in it, why use it?
Another thing to consider regarding Oracle and Rails: none of the full text searching plug-ins for Rails support Oracle at this time. Indeed, pretty much only MySQL and PostgreSQL are well-supported by the various full-text options.
This is not to say you won't be able to do full-text searching if you use Oracle, just that you will not be able to use any of the existing plug-ins that make doing so simple.
I'm currently using Oracle with some Rails applications. There are both standard ActiveRecord and JDBC-ActiveRecord adapters (I'm hosting some apps with JRuby) and for the most part, things work great.
I would suggest using the enhanced ActiveRecord Oracle adapter though.
http://rubyforge.org/projects/oracle-enhanced/
But if you're not locked into Oracle, go with Postgres or MySQL.

Resources