Forum: Ruby on Rails unused composed_of bits

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dean (Guest)
on 2006-03-03 05:46
(Received via mailing list)
I implemented a Temperature model for my application.  It is pretty
basic,
composed of a temperature, a unit and some conversion methods.  In my
app model
I decided to store temperature values in Fahrenheit, so there is no need
to
remember the units between invocations.  I am having a little trouble
composing
my app model.

A little bit of my temperature class:

class Temperature
# Composed of a float with 1 sig fig and units of Celsius, Fahrenheit or
Kelvin
# Implements to_s, to_html and a number of conversions and bang
conversions

attr_reader :temp, :units

def initialize( temperature, units = "F" )
   unless valid_units?(units)
     units = "F"
   end
   @temp = temperature = ((10 * temperature).to_f.round)/10.0
   @units = units.upcase
end

def to_c
   convert_to("C")
end
... etc.

So in my app model:

class Profile < ActiveRecord::Base
   composed_of :temperature, :class_name => Temperature,
               :mapping => [ :temp, :temp ]

   def before_save
     self.temperature.to_f!
   end

The table corresponding to Profile has a column named temp.  What
happens to
the units?  Is it merely ignored?

When I navigate to /profiles/new I get:

undefined method `temperature.temp' for #<Profile:0xb7395660>

Extracted source (around line #10):

7:   </div>
8:   <div class="form-element">
9:     <label for="profile_temperature">Temperature</label>
10:     <%= text_field 'profile', 'temperature.temp' -%>&deg;<br />

Controller:
def new
   @profile = Profile.new
   @profile.temperature = Temperature.new(0)
   render :layout => !request.xhr?
end

How do I create a text_field for just the temp part of the Temperature
object?
  Or should I just cut to the chase and do a <%= text_field 'profile',
'temp'
%> and create the Temperature object in the controller?

Regards,
--Dean - Unscrambler of eggs
--
Take your time, take your chances
[3278.7 km, 273.4] Apparent Rennerian
------------------------------------------------------------------------------
It matters not how strait the gate / How charged with punishment the
scroll
I am the master of my fate / I am the captain of my soul.    -- Invictus
--
                                                         -- William E
Henley --
This topic is locked and can not be replied to.