For a new project my company is considering using Amazon SimpleDB to simplify data storage. The app is a simplistic web dashboard that will be created using Ruby on Rails. What I'm wondering, though, is if I can still use associations like has_many and belongs_to while using SimpleDB as the backend.
For instance, the application has users who log in and have messages. In "normal" Rails with MySQL I could easily do this with the built-in associations. Will using SimpleDB as the storage engine prevent me from doing this? Also, will I have to re-implement the login functionality? I was initially going to use the restful_authentication plugin, but I have no idea if I will have to rewrite it to work with SimpleDB.
I think SimpleDB will be a problem for your wishes....
(and i tink you have to reimplement the login authentication as well, because its based on ActiveRecord like all the other models)..
You should check out SimpleRecord, just like ActiveRecord, but using SimpleDB for backend.
Related
Is it possible to use multiple couchdb databases for data in rails app while using the CouchRest Model gem? Right now in my rails app I specify the one couchDB database in config/couchdb.yml. I was wondering if it possible to have more than one couchdb databases being used by one rails app?
You might want to check out this answer - it worked for my case where I wanted a database for each user
Recommended use of couchrest model in a multi-tenant app
I am planning on using Devise and Apartment in my upcoming application to create subdomains for each organization that creates an account. I would like to host my application on Heroku, but ran across the following quote:
The most common use case for using multiple schemas in a database is
building a software-as-a-service application wherein each customer has
their own schema. While this technique seems compelling, we strongly
recommend against it as it has caused numerous cases of operational
problems. For instance, even a moderate number of schemas (> 50) can
severely impact the performance of Heroku’s database snapshots tool,
PG Backups.
What technique would work well with Heroku to host basecamp-style subdomains in rails 4 where many users can log in to the subdomain which they are part of?
If Heroku does not work, what other PaaS options are there that would do this well?
Domain
Firstly, you need to be sure that you're using your own custom domain for the subdomains.
Heroku's standard xxx.herokuapp.com won't be able to handle another subdomain on top of that - so you'll basically need to use your custom domain from the get-go
It will be good to reference this documentation for more information!
Multi Tenancy
Although I don't have experience with PGSQL's schemas, I do have some with multi tenancy as a whole.
There are a number of great resources here:
Basecamp-style Subdomains (by DHH)
Multitenancy Railscasts (Pro)
Apartment Gem Documenatation
Essentially, multi-tenancy is just a way to scope the data so that it's only the tenant's that you see / interact with. In the sense of the DB, the two ways to achieve this are either to use different DB's (as you would with MYSQL), or use a schema (like with PGSQL)
Whilst I can't give you a direct fix for your issue, I can help you with some ideas:
Models
One way to achieve multi-tenancy, especially with the likes of MYSQL, is to do it through the model:
How do i work with two different databases in rails with active records?
#lib/admin.rb
class Admin < ActiveRecord::Base
self.abstract_class = true
establish_connection "#{Rails.env}_admin"
end
#app/models/option.rb
Class Option < Admin
# do stuff
end
This works very well for us, although we have not got it working for scoped accounts yet. We've been thinking of setting a ##class_variable for the Account or something, but haven't been working on that right now.
This works very well for MYSQL - powered databases, but also means you'll have to create db's for every account, which will not work with PGSQL (as far as I'm aware)
PGSQL Schemas
I feel this is kind of a cheat way to do this, as all the data is still stored in 1 database - it's basically just scoped around different types of data.
The problem here is that real multi tenancy should be where you completely separate the user's data, so you could cut it out of the app completely if they wanted. From a security & access perspective, it's the most flexible & modular way.
The problem for Heroku is they can only use one database (they give everyone access to their AWS database instances), meaning they can't allow you to create 50+ free databases (it just won't work very well).
You can, of course, use your own stack to create the databases you require, but in terms of PGSQL, it's just about creating the schemas for your data & then using something like -Apartment to make it happen:
PostgreSQL works slightly differently than other databases when
creating a new tenant. If you are using PostgreSQL, Apartment by
default will set up a new schema and migrate into there. This provides
better performance, and allows Apartment to work on systems like
Heroku, which would not allow a full new database to be created.
I'm developing a iOS app and want it to talk to a rails server.
I was wondering the normal approach for making API's for iOS consumption is? My original plan was to develop the site in rails with the functionality and the way I want it to work and then take that site and create an API for it. This seems overkill considering I don't want a web version of the app.
I'm interested to hear if anyone has had any experience with approaching this or how they would go about it.
I know I could develop the API standalone but am unsure how to develop the site functionality within a standalone API without views e.g using the rails-api gem.
Sorry if this questions is not explained well as I'm still relatively new to rails.
Thanks.
Assumption: rails 4+, you want the API to be JSON based.
If you use scaffold to create your model objects the you've got the API pretty much written for you. I mean it will create the views for you, and it's up to you to do any changes you want to the controller.rb (probably not) and to the view (action.json.jbuilder).
http://guides.rubyonrails.org/v3.2.13/getting_started.html
(go to) 5 Getting Up and Running Quickly with Scaffolding
A common change you'll make will be formatting a datetime property from your Rails model to be a certain format (lets go with unix timestamp), so you put those changes in your action jbuilder file, i.e
app/views/person/show.json.jbuilder
json.extract! #person, :first_name, :last_name, :id
json.date #person.date_of_birth.to_i
So now when you browse to
/person/23.json
you'll get
{
"first_name":"Rails",
"last_name":"is great",
"id": 23,
"date_of_birth": 1395101106
}
In summary, use
rails generate scaffold model_name property:string other_property:int
for your model objects
Use ActiveModelSerializers with the JSONAPI adapter, and jsonapi-ios on the iPhone. That way you have a well thought out JSON format out of the box.
For authentication I'd recommend Devise with devise_token_auth, and for roles the cancancan gem.
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).
I'm building a Rails 3 application on CouchDB (using SimplyStored gem) and I'd like to use some existing gem/plugin for authentication, instead of building it from scratch.
Problem is, I can't find anything that works smoothly for CouchDB, everything assumes that you're running on ActiveRecord. Do you have any tips?
Try Using CouchRest_Model.
So, in the end I've used heavily customized Clearance gem for authentication. It's very flexible, so you're able to use it even for NoSQL database - but you have to rewrite most of the functionality (not by monkey-patching, all within limits of Clearance customization), which probably takes more effort that to write whole authentication from scratch. While a good-enough solution, but I'm sure there better ones.