2

I've just started learning Ruby on Rails and I was wondering if there is a way by which I can access a variable defined in a model's method. I have a controller where I want to access the variable. I have a model

class abc
 def score
  sum = 10
 end
end

And there is a controller

class FirstController < ApplicationController
 def get_score
 end
end

So I want to get the value of variable sum in method get_score. How can I achieve this?

1
  • you can get score using any object of class abc. Commented Dec 29, 2015 at 5:46

3 Answers 3

2

This is an object orientated programming question; you're not calling a variable from your model, you're accessing either an attribute or an instance value (very important in terms of scoping etc) from a class.

--

You'll either need to make the variable a class variable, invoke it as an instance method, or have class method to return it:

#app/models/model.rb
class Model < ActiveRecord::Base
   cattr_accessor :sum #-> Model.sum class variable (static)
   @@sum = 10

   def self.sum
     10 #-> Model.sum class method (static)
   end
end

What you do depends on what type of data you're looking to return.

  • If the data is static, use a class method / variable
  • If the data is dynamic, use an instance method

Above is the code you'd use if you want to return a static value.

If you wanted to return a dynamic value, you'd use:

#app/models/model.rb
class Model < ActiveRecord::Base
   def score
      self.games * self.goals # -> @model.sum instance method (dynamic)
   end
end

--

The difference is that if you use a class value, it is only available through initialization of the class. IE you can call Model.sum and have access to the record.

Instance methods / values are only accessible through an instance of the class:

@model = Model.find 5 #-> creates a new instance of the class
@model.sum #-> pulls the dynamic sum value of the class instance

Fix

In your case, you'd be best using an instance method:

#app/models/abc.rb
class Abc < ActiveRecord::Base
   def score
      10
   end
end

#app/controllers/first_controller.rb
class FirstController < ApplicationController
   def get_score
      @abc = Abc.new
      @abc.sum #-> 10
   end
end

This way, your data will be dynamic, allowing you to manipulate it etc.

Sign up to request clarification or add additional context in comments.

Comments

2

In ruby method returns the last statement value if return statement is not return explicitly. You can access the score as follows:

class FirstController < ApplicationController
 def get_score
      Abc.new.score
 end
end

Remember the class name is always starts with capital letter.

Comments

1

In your abc.rb model file:

class Abc < ActiveRecord::Base
  def self.score
    sum = 10
  end
end

Now you can access score method using Model name anywhere like.

class FirstController < ApplicationController
 def get_score
    Abc.score # you will get 10 here
 end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.