background preloader

DB

Facebook Twitter

Using CORS. Introduction APIs are the threads that let you stitch together a rich web experience. But this experience has a hard time translating to the browser, where the options for cross-domain requests are limited to techniques like JSON-P (which has limited use due to security concerns) or setting up a custom proxy (which can be a pain to set up and maintain). Cross-Origin Resource Sharing (CORS) is a W3C spec that allows cross-domain communication from the browser. By building on top of the XMLHttpRequest object, CORS allows developers to work with the same idioms as same-domain requests.

The use-case for CORS is simple. Imagine the site alice.com has some data that the site bob.com wants to access. This type of request traditionally wouldn’t be allowed under the browser’s same origin policy. As you can see from this example, CORS support requires coordination between both the server and client. Making a CORS Request This section shows how to make a cross-domain request in JavaScript. xhr.send(); Active Record Query Interface. If you're used to using raw SQL to find database records, then you will generally find that there are better ways to carry out the same operations in Rails. Active Record insulates you from the need to use SQL in most cases.

Code examples throughout this guide will refer to one or more of the following models: All of the following models use id as the primary key, unless specified otherwise. Active Record will perform queries on the database for you and is compatible with most database systems (MySQL, PostgreSQL and SQLite to name a few). Regardless of which database system you're using, the Active Record method format will always be the same. 1 Retrieving Objects from the Database To retrieve objects from the database, Active Record provides several finder methods. The methods are: bindcreate_withdistincteager_loadextendingfromgrouphavingincludesjoinslimitlocknoneoffsetorderpreloadreadonlyreferencesreorderreverse_orderselectuniqwhere 1.1 Retrieving a Single Object 1.1.1 find 1.1.2 take :start.

4.5. Populating the Database with seeds.rb. With the file db/seeds.rb, the Rails gods have given us a way of feeding default values easily and quickly to a fresh installation. This is a normal Ruby program within the Rails environment. You have full access to all classes and methods of your application. So you do not need to enter everything manually with rails console in order to make the records created in the section called “create” available in a new Rails application, but you can simply use the following file db/seeds.rb: Country.create(name: 'Germany', population: 81831000) Country.create(name: 'France', population: 65447374) Country.create(name: 'Belgium', population: 10839905) Country.create(name: 'Netherlands', population: 16680000) You then populate it with data via rake db:seed.

I use the file db/seeds.rb at this point because it offers a simple mechanism for filling an empty database with default values. Generating seeds.rb From Existing Data We create our own little rake task for that. Active Record Associations. 1 Why Associations? Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for customers and a model for orders. Each customer can have many orders. Without associations, the model declarations would look like this: Now, suppose we wanted to add a new order for an existing customer. Or consider deleting a customer, and ensuring that all of its orders get deleted as well: With Active Record associations, we can streamline these - and other - operations by declaratively telling Rails that there is a connection between the two models.

With this change, creating a new order for a particular customer is easier: Deleting a customer and all of its orders is much easier: To learn more about the different types of associations, read the next section of this guide. 2 The Types of Associations In Rails, an association is a connection between two Active Record models. Active Record Migrations. 1 Migration Overview Migrations are a convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent. You can think of each migration as being a new 'version' of the database. A schema starts off with nothing in it, and each migration modifies it to add or remove tables, columns, or entries. Here's an example of a migration: This migration adds a table called products with a string column called name and a text column called description. Note that we define the change that we want to happen moving forward in time.

On databases that support transactions with statements that change the schema, migrations are wrapped in a transaction. There are certain queries that can't run inside a transaction. If you wish for a migration to do something that Active Record doesn't know how to reverse, you can use reversible: 2 Creating a Migration will generate. Active Record Query Interface. Active Record Migrations.