My experience making websites has been in line with this sentiment: Unless your data objects live in complete silos from one another (and you're sure they will be that way for the foreseeable future), you'll probably be better off using a relational database like Postgres. Another example is when you store a user’s name in various places for easy access, but when the user updates their name you’re forced to find all of those places to make sure their information is up to date. An example of this is when data that was thought to be in a silo needs to cross boundaries (what relational DBs are great at). We do have excellent choices nowadays,īut with the growing support and new features it is hard to say no in most of the cases.An article by Sarah Mei titled " Why you should never use MongoDB" discusses the issues you’ll run into if you try to use a NoSQL database when a relational database would be far superior. Postgres is a solid choice for open source database when pairing it with Ruby on Rails. This will let you make complex queries easily using the power Postgres provides! Conclusion ~* is the operator for case insensitive and ~ is the one for case sensitive.įor Rails 4 and beyond the general query format is Flag.where("name ~* ?", "regex") It is really easy to use regex in a query using Rails and Postgres. We used it already and they can be very powerful. To end this you just need to delete the actual schema and recreate it with bundle exec rake db:migrate. This is as easy as adding a line inside config/application.rb:Ĭonfig.active_record.schema_format = :sql We will have to tell Rails to use SQL instead of plain Ruby when storing the schema. Rails doesn’t know how to handle check constraints, and because of this ourĬhanges will not show up in the db/schema.rb. Now we can tell that our flags will respect the rules.Īnyway, this leads to a problem. Here we are using regular expression to match our desires. must fail our validation.įor this we create a migration: class AddNameConstraintToFlags < ActiveRecord :: Migration def up execute <<- SQlĪDD CONSTRAINT no_flags_starting_with_vowelsĬHECK ( namel ~* ' \A (?=)(?=)' )ĭROP CONSTRAINT no_flags_starting_with_vowels So pretty much countries like Angola, Ukraine and etc. Imagine that, for some reason, we just want to add flags with a name that does not start with vowels. Let’s use our previous example again, the Flag model. It is easy to set a constraint inside a migration. Is perfect in this situation, blocking the error to reach our database. Update_attribute, that will bypass your validations, and this can cause some troubles if you are not aware. Here we will be using a feature called “check constraint” to assist Rails validations. where ( 'colors ?
0 Comments
Leave a Reply. |