4/9 Tags Controller, Tag.all explanation


#1

Hello there !

I need help to understand this :

class TagsController < ApplicationController
  def index
    @tags = Tag.all
  end
end

I'm confuse about the way "Tag.all" works. How does "Tag.all" (singular with a capitalize T) relates with a db model named "tags" (plural with no capitalized letter)

This code works but i can't understand why :no_mouth:

Thanks !


#2

Hey Tortue,

This is slightly confusing at first :slight_smile:
The model, which is Tag (singular), as what holds stuff in the database. The controller - Tags, plural - is where you create views to show in the browser.

Because of this, since you're selecting database content (all of the tags), you need to use Tag.all, instead of Tags.all, which would make more sense :slight_smile:

Does this help?


#3

It's a start zytsvan :wink:

I understand now that Tag.all calls the model named "Tag". This model talks with the db named "tags"

If i understand correctly how it works :
1) We generate a model name "Test"
2) It will generate a migrate file name "20160320192154_create_tests.rb"
3) This file permits to create a table name "tests"

I assume ruby on rails automatically links a model with a db that has the same name plus an "s" at the end. If we create a models name "Xyzss", the migrate file will creates a database with the same name plus an "s" : "xyzsss"

Am i right ? :grin:


#4

@tortue.82

I think so, that sounds correct to me.

Actually, Rails will automatically try to detect if you have a plural model or singular controller name, and warn you about it if you do. So I think if you created a model called “Xyzss”, it would warn you and make sure you really wanted “Xyzss” as your model name, but then create it just exactly like that if you do want it that way :slight_smile:


#5

Thanks for your help. I have a better understanding of the logic behind Rails now ! :wink: