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.
scoreusing any object of classabc.