# Accessing a variable in the view

**URL:** <https://www.ruby-forum.com/t/accessing-a-variable-in-the-view/146792>\
**Category:** Rails\
**Created:** [August 25, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/accessing-a-variable-in-the-view/146792 "2008-08-25T15:26:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 25, 2008, 3:26pm UTC](https://www.ruby-forum.com/t/accessing-a-variable-in-the-view/146792/1 "2008-08-25T15:26:51Z")

</div>

Now I’ve decided I want to access factor in my view…

but it’s blank when i try   
Factor: \<%= @course.factor %\>

How do I do it?

here’s my model…

* * *

class Course \< ActiveRecord::Base

has\_one :enquiry

attr\_accessor :duration\_in\_weeks  
attr\_reader :factor

def self.all\_courses  
find(:all)  
end

def self.active\_courses  
# find(:all)  
# find(:all, :conditions =\> [“active = ?”, true], :order =\>  
‘created\_at DESC’, :limit =\> 5)  
find(:all, :conditions =\> {:archive =\> false}, :order =\>  
‘display\_order ASC’, :limit =\> 100)  
end

def self.old\_courses  
# find(:all)  
# find(:all, :conditions =\> [“active = ?”, true], :order =\>  
‘created\_at DESC’, :limit =\> 5)  
find(:all, :conditions =\> {:archive =\> true}, :order =\> ‘created\_at  
DESC’, :limit =\> 100)  
end

def discounted\_price  
@factor = case @duration\_in\_weeks  
when 1…4  
1.0  
when 5…12  
0.9  
when 13…24  
0.8  
when 25…47  
0.7  
else # from 48 and up  
0.6  
end

```
total_price * @factor

```

end

def total\_price  
(@duration\_in\_weeks \* price\_per\_week) + registration\_fee  
end

def duration\_in\_weeks=(duration\_in\_weeks)  
@duration\_in\_weeks = duration\_in\_weeks.to\_i  
end

end

---

<div class="post-metadata">

**Author:** ![bingobob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e480ec/32.png) [@bingobob](https://www.ruby-forum.com/u/bingobob)\
**Post date:** [August 25, 2008, 3:45pm UTC](https://www.ruby-forum.com/t/accessing-a-variable-in-the-view/146792/2 "2008-08-25T15:45:27Z")

</div>

On Aug 25, 2008, at 9:26 AM, bingo bob wrote:

> Now I’ve decided I want to access factor in my view…
> 
> but it’s blank when i try   
> Factor: \<%= @course.factor %\>

Until the discounted\_price method is called, @factor has no value.

You could pull the case statement that sets @factor into its own  
method and call that ‘factor’. Then you can omit the attr\_reader  
(since you defined the method yourself) and @course.factor will do  
what you originally expected.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
