Initializing objects of a model

current_user

This command:

rails g model Product name quantity:integer

generates a model Product with two fields: name, which is a string, and quantity, which is an integer.

  1. Are the fields instance variables of Product?
  2. If so, when and where and how are they initialized?
  3. When creating a Product instance in a controller:

eg:

    def new
      @product = Product.new
    end

where is the initialize method called?

jvillian

To be a little more precise...

When you do:

rails g model Product name quantity:integer

You are not adding a product table to your database. You are creating a migration and a model (and perhaps some tests, depending...). You'll see something like:

  invoke  active_record
  create    db/migrate/20171129070104_create_products.rb
  create    app/models/product.rb

When you do:

rake db:migrate

THEN you're adding a table to your database. You'll see something like:

== 20171129070623 CreateProducts: migrating ==============
-- create_table(:products)
   -> 0.0179s
== 20171129070623 CreateProducts: migrated (0.0180s) =====

Once migrated, your products table will have columns named name and quantity.

To your questions:

Are the fields instance variables of Product?

Roughly, yes. An instance of Product will have attributes for name and quantity. So, if you do (in console):

p = Product.new

You will see:

> p = Product.new
 => #<Product id: nil, name: nil, quantity: nil, created_at: nil, updated_at: nil>

If so, when and where are they initialized?

Instance attributes are initialized when calling new on a model class. If you call new with no arguments, then the attributes are initialized to nil.

You can also call new with arguments, such as:

> Product.new(name: 'foo', quantity: 2)
 => #<Product id: nil, name: "foo", quantity: 2, created_at: nil, updated_at: nil>

Where are they initialized? Anywhere you want. There are varying ideas about practices that are "good" and practices that are "bad". But, that's a much longer discussion.

Personally, I never interact (e.g., initialize, save, update, delete) with my models in my controllers or views. I have a custom class called managers and that is the only place I interact with my models. But, I am very odd in this regard and you should consider my practice "anomalous".

When a creating a Product instance in a controller, where is the initialize method called?

As pointed out elsewhere, it is called when new is called. There are some subtleties, and you might want to look into ActiveRecord::Callbacks.

Also, as pointed out elsewhere, you can override initialize for any number of reasons.

By the way, new and initialize are not unique to models. It is very common to use them in plain old ruby objects.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Initializing objects dynamically

From Dev

Initializing nested Javascript objects

From Dev

Initializing an Array to Store Objects

From Dev

Initializing multiple objects at once?

From Dev

initializing objects in delphi

From Dev

initializing in loop array with objects

From Dev

dojox/app model is not initializing

From Dev

Initializing objects with macros for integer constants

From Java

Initializing list of objects using polymorphism

From Dev

Initializing objects used in routes with Flask

From Dev

Initializing objects of my class with a string

From Dev

Initializing many objects in an elegant way

From Dev

initializing 2Darray of Objects

From Dev

Initializing model parameters in pytorch manually

From Dev

compose objects without initializing objects that are not in hash

From Dev

Initializing Cython objects with existing C Objects

From Java

Lombok @SuperBuilder is not initializing my class objects

From Dev

Making/Initializing Array of Struct Like Objects in Java

From Dev

Declaring and initializing static objects at class scope

From

Is there a pattern for initializing objects created via a DI container

From Java

declaring/initializing primitives equal to creating new objects

From Java

Unable to master constructors and classes in java (initializing objects)

From Dev

Initializing 2 objects that require each other as parameters

From

Good or bad practice? Initializing objects in getter

From Dev

initializing an array from objects makes NullPointerException

From Dev

JS browser game - avoid initializing all objects

From Dev

Having trouble initializing an ArrayList of Objects inside a constructor

From Dev

Error when initializing vector of objects with size

From Dev

Initializing objects into an arrayList from test class

Related Related

  1. 1

    Initializing objects dynamically

  2. 2

    Initializing nested Javascript objects

  3. 3

    Initializing an Array to Store Objects

  4. 4

    Initializing multiple objects at once?

  5. 5

    initializing objects in delphi

  6. 6

    initializing in loop array with objects

  7. 7

    dojox/app model is not initializing

  8. 8

    Initializing objects with macros for integer constants

  9. 9

    Initializing list of objects using polymorphism

  10. 10

    Initializing objects used in routes with Flask

  11. 11

    Initializing objects of my class with a string

  12. 12

    Initializing many objects in an elegant way

  13. 13

    initializing 2Darray of Objects

  14. 14

    Initializing model parameters in pytorch manually

  15. 15

    compose objects without initializing objects that are not in hash

  16. 16

    Initializing Cython objects with existing C Objects

  17. 17

    Lombok @SuperBuilder is not initializing my class objects

  18. 18

    Making/Initializing Array of Struct Like Objects in Java

  19. 19

    Declaring and initializing static objects at class scope

  20. 20

    Is there a pattern for initializing objects created via a DI container

  21. 21

    declaring/initializing primitives equal to creating new objects

  22. 22

    Unable to master constructors and classes in java (initializing objects)

  23. 23

    Initializing 2 objects that require each other as parameters

  24. 24

    Good or bad practice? Initializing objects in getter

  25. 25

    initializing an array from objects makes NullPointerException

  26. 26

    JS browser game - avoid initializing all objects

  27. 27

    Having trouble initializing an ArrayList of Objects inside a constructor

  28. 28

    Error when initializing vector of objects with size

  29. 29

    Initializing objects into an arrayList from test class

HotTag

Archive