Rails - Models

rails

How to define a new model?

Rails provides a generator for creating models, which most Rails developers tend to use when creating new models. To create the new model, run this command in your terminal:

bin/rails generate model Article title:string text:text

With that command we told Rails that we want a Article model, together with a title attribute of type string, and a text attribute of type text. Those attributes are automatically added to the articles table in the database and mapped to the Article model.

Rails responded by creating a bunch of files. For now, we're only interested in app/models/article.rb and db/migrate/20140120191729_create_articles.rb (your name could be a bit different). The latter is responsible for creating the database structure.

How can I generate a model without creating a migration file for it?

I want to skip the creation of a migration file while generating a model. I already have the database created.

You can generate the model normally and then delete the migration file, or just create model ruby file and you are done.

Or use the —skip-migration flag:

rails g model User --skip-migration

What happens when you use the generator to define a model?

Rails responded by creating a bunch of files. For now, we're only interested in app/models/article.rb and db/migrate/20140120191729_create_articles.rb (your name could be a bit different). The latter is responsible for creating the database structure.

After we use the generator to generate a model for a given resource, where can we find the file that contains the model definition?

  • db/migrate/20140120201010_create_comments.rb: Migration to create the comments table in your database (your name will include a different timestamp)
  • app/models/comment.rb: The Comment model
  • test/models/comment_test.rb: Testing harness for the comments model
  • test/fixtures/comments.yml: Sample comments for use in testing

How to generate a model that belongs to another model?

bin/rails generate model Comment commenter:string body:text **article:references**

The above command generate the Comment model which belong to the Article model. A "belong to" relationship is a one-to-many relationship. One article can have many comments.

Take a look at app/models/comment.rb:

class Comment < ActiveRecord::Base
  belongs_to :article
end

This is very similar to the Article model that you saw earlier. The difference is the line belongs_to :article, which sets up an Active Record association.

After using the generator to generate a belong_to relationship on one model, what do we have to do to update the other model as a has_many relationship?

A belongs_to relationship defined in one model should implicitly imply a has_many relationship in the other model. I am not quite sure why we have to update the other model.

You'll need to edit app/models/article.rb to add the other side of the association:

class Article < ActiveRecord::Base
  has_many :comments
  validates :title, presence: true,
                    length: { minimum: 5 }
end

These two declarations enable a good bit of automatic behavior. For example, if you have an instance variable @article containing an article, you can retrieve all the comments belonging to that article as an array using @article.comments.

Why do we need to declare our models with belongs_to or has_many relationship?

These two declarations enable a good bit of automatic behavior. For example, if you have an instance variable @article containing an article, you can retrieve all the comments belonging to that article as an array using @article.comments.

After we manually make a change to a model file, do we have to use a generator or manually create a migration file?

I haven't work enough with Rails to really know the answer to this question. I guess that not all changes to the model file require a migration, and we probably would never be required to use a generator or manually create a migration file.

Examples:

tryruby.org

t = Tweet.find(3)

t = Tweet.new
t.status = “I ate 3 brains”
t.save

t = Tweet.find(3)
t.zombie = “EyeballChomper”
t.save

t = Tweet.find(3)
t.destroy

t = Tweet.new(
    status: “I ate 3 brains”,
    zombie: “Jim”
)
t.save

t = TableName.new
t.key = value
t.save

Tweet.create(hash)

Tweet.find(2)
Tweet.find(3,4,5)

Tweet.first
Tweet.last
Tweet.all
Tweet.count
Tweet.order(:zombie)
Tweet.limit(10)
Tweet.where(zombie: “ash”)

Tweet.where(zombie: “ash”).order().limit(10)
Tweet.where(zombie: “ash”).first

t = Tweet.find(2)
t.attributes = {
    status: “Can I munch your eyeballs?”,
    zombie: “EyeballChomper”
}
t.save

t = Tweet.find(2)
t.update(
    status: “Can I munch your eyeballs?”,
    zombie: “EyeballChomper”
)

Tweet.find(2).destroy

Tweet.destroy_all
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License