Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of the @ variables inside Rails models

I've worked through a few Rails applications which are using @ attribute variables (which are already attr_accessible) in the models. I was having a tough time getting information on this but from what I gather name and @name are the same thing in a model but im probably incorrect on this.

How do these @ variables work and why would one ever use the @ symbol variables?

like image 349
Weston Ganger Avatar asked Dec 08 '22 23:12

Weston Ganger


1 Answers

To add to the current answers, @instance_variables are from object oriented programming...

In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each instantiated object of the class has a separate copy, or instance.


OOP

"Normal" programming has variables as pieces of data -- strings, integers etc. These are independent & can only interact as part of other functions, depending on their scope.

Object Oriented programming (OOP) can treat variables as pieces of editable data (known as classes). These classes can be invoked, edited and most importantly interacted...

#app/models/product.rb
class Product < ActiveRecord::Base
  #-> class
end

def new
   @product = Product.new #-> creating a new instance of the class
end

Ruby/Rails is object oriented; it works by giving you a series of objects to load & interact with. The most notable example is with game programming:

enter image description here

The way object orienting works is to invoke/initialize an instance of a class (in our case Product), allowing you to manipulate it.

Class instances hold the object in memory, allowing you to perform actions on the class itself. To do this, you'd store the instance of the class in a variable, allowing you to interact with the variable itself:

@product = Product.new
@product.save

--

Instance variables are only valid within the context of the class:

# app/controllers/products_controller.rb
class ProductsController < ApplicationController
  def show
    @product = Product.new #-> @product only available within ProductsController
  end
end

The controllers in Rails are classes, invoked through a rack request:

Request > Rack > Routes > ProductsController.new(request).show > Response

If you want your @instance_variable available in all methods of the class, it has to be at instance level...

# app/controllers/products_controller.rb
class ProductsController < ApplicationController
  def show
    @product = Product.new
    product_save
  end

  private

  def product_save
    @product.save #-> @product available in instance of ProductsController class
  end
end

The most common use for @instance_variables are to store / manipulate instance-centric data. A good example (for our Product example) could be the stock level:

#app/models/product.rb
class Product < ActiveRecord::Base
  def stock
     @qty / @orders
  end
end

Because you can use getter/setter methods within Ruby, you can define the instance values of a class, accessing them through other instance data:

@product = Product.find x
@product.stock #-> outputs stock value for that product
like image 130
Richard Peck Avatar answered Dec 20 '22 10:12

Richard Peck