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.
Related
I have found this Q-A Why does ActiveRecord in Rails not support Multiple Table Inheritance?
And now i have a question according to CdotStrifeVII's answer
Polymorphic associations fill that role for most use cases
do you agree with this?
As for me, polymorphic is more like STI implementation...
How rails polymorphic mechanism can be used to implement MTI?
I have a problem that I have been trying to solve for a while now. I have 4 different Rails applications using the same database, meaning they need to use the same models and have the same migrations. I initially solved the problem by creating a Rails engine packaged into a gem, which then carries all the models and migrations with it. Now I realize that there are pieces of functionality that only one application needs, but the others do not - like for example the admin application needs to have methods for providing sortable tables for all models - the other applications do not need this functionality at all.
So my idea was to find a way where I can provide the "base" models from the gem, while augmenting these base models in my specific applications to add additional functionality when needed. What I tried first was inheritance:
class User < Base::User
end
This does not work though, because now you have 2 User models in your load path (User and Base::User) and when querying associations, it always picks the "closest" class for the associated record class - meaning when you have an Model::Account which belongs_to :user, it will pick Model::User as the association class, not User. I tried reversing the AR type compute method but this only resulted in more problems.
I can technically provide all of my models from the base engine (gem), but the issue here is that how do i extend these models in my application? .class_eval feels really really dirty, inheritance does not work, providing base functionality as mixins means the "base" models do not feel and look like models at all. My goal would be to cause as little friction as possible for the other developers, I want them to be able to define their models in the gem like they do normally and then also have an easy way to extend that functionality in other applications.
Has anyone solved this problem before or have any suggestions? Or how do you guys solve this problem in your larger applications? Any help would be appreciated.
This is mentioned in the Rails guides. It describes class modification with the Decorator pattern.
I'm working on Ruby on Rails 3 application. Already, I have implemented authentication and authorization based on Devise and Cancan. In scope of this I have implemented "Member" object that can have at least 2 different roles - "User" and "Company". Physically, I'd like to represent these roles as two derived classes from Member, such as User and Company.
Should I use single table inheritance or multiple table inheritance. In case of multiple table inheritance what plugin I should use ?
I know it is not the answer to your question but I think you should reconsider using something else than inheritance.
Inheritance is a great OO mechanism but is very often overused. Most often you can do a better job with an association and some kind of Strategy Pattern than with inheritance.
Activerecords are much more compatible with that kind of design than inheritance and it is also much more respecting the Single Responsibility Principle which means it's easier to test in isolation.
If you want to go with the the Strategy pattern in your activerecord model, polymorphic association can be the way to go (this allows you to even have stateful strategies)
I'm new to Rails and am very keen to learn and understand it inside out. While I was doing a small example I created my own custom-built model class for which I wanted to have a form. I also wanted to apply all the basic validations on all attributes as well when user input the values through the form. But I could not find much help from Rails out-of-the-box features like it provides for ActiveRecords.
The reason why I would like not to have an ActiveRecord and rather a simple custom-built model is that I don't want to persist it at all. In fact my object doesn't have anything to do with database.
What is the best practice to build custom models classes, add validations to their attributes and associate them with appropriate views?
Thanks a lot in advance for your help.
I think you're looking for this:
http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/
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.