-1

Basic Sql In Ruby On Rails

Basic Sql In Ruby On Rails

-SQL.png

Structured and relational databases are everywhere. It is often said that web applications are front end interfaces to a back end database. In a stateless protocol like HTTP, your database keeps the state and serves as the persistence layer. It is the brains behind the machine.

In the traditional sense of the MVC design pattern, your data model solves the problem. The way you architect the database has a direct correlation to the overall solution. If you end up with a database full of convoluted bloat and duplication, your front end code will mirror exactly that.

So, in this article I would like to take a look at SQL databases. As it turns out, Rails makes this easy. The framework makes it easy to think about real world problems as data models and relationships.

The relationships in your data model turn models into real world “objects”. The awesomeness in this is how well it fits within an OOP paradigm. It simplifies code bases, and makes the architecture intelligible and agile.

My hope is that, by the end of this article, you’ll gain an appreciation for structured and relational databases. This will help you understand why your back end structured storage is critical to a sound architecture.

And we all want to design a beautiful architecture, right? Let’s get started.

Begin

You are more than welcome to follow along if you like. I’ll have the code linked at the end of this article.

To get started, type up this canonical command:

$ rails new sql_in_ror

For this particular example, it’s a run of the mill posting system. It consists of Users, Posts, and Categories. From this, we’ll define relationships and get down and dirty with SQL.

The primary focus here is the Model within the MVC design pattern.

So, type up:

$ rails g model User name:string
$ rails g model Post title:string  user:references:index
$ rails g model Category name:string
$ rails g migration CreateCategoriesPostsJoinTable categories:index posts:index

Now we can finish it with code:

$ rake db:migrate

Let's contitnue it and go to app/models/category.rb

class Category < ActiveRecord::Base
   has_and_belongs_to_many :posts
end

and in app/models/post.rb

class Post < ActiveRecord::Base
  belongs_to :user
  has_and_belongs_to_many :categories
end

Finally in app/models/user.rb

class User < ActiveRecord::Base
  has_many :posts
end

Relationships

With Rails, your entire database is setup. Now, it is time for us to delve deep into what just happened. The framework does a good job of abstracting it away. Alas, for us, it is time to figure out how this all works.

Type up:

$ rails console
> post = Post.first
> post.categories
> category = Category.first
> category.posts
> user = User.first
> user.posts

Query

With ruby on rails provide a lot of the methods to run code performs as SQL:

bind
create_with
distinct
eager_load
extending
from
group
having
includes
joins
limit
lock
none
offset
order
preload
readonly
references
reorder
reverse_order
select
uniq
where

you can find out more about in Ruby On Rails API.

Example:

User.where "name = 'samnang'"

User.include :address, :age

User.includes(:post).where("post.title = ?", "example")

Function

In this case, you maybe wonder about that, why I called it that Function, with Ruby on rails you can create write code to query sql, more and more than that you can marcro your code sql in to one function and call it in the other place of project. It is called definite or Scope Example:

User.where "age < 10"

You can marcro the code above into one fuction to use it.

 // using definite
 def self.smaller_than age
    where "age < ?", age
 end

 // using scope
 scope :smaller_than, ->(age){where("age < ?", age)}

$ User.smaller_than 10
$ User.smaller_than 25

Conclusion

That is a wrap. In this article, it is base of Sql in ruby on rails. However you can find out about that in api of ruby on rails with name of function above. I hope it can help you guy if you are beginer SQL in Ruby On Rails.

Document


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí