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 <br>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 Biedenharn http://agileconsultingllc.com
Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org
> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---