Forum: Ruby on Rails model methods

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.
43ec5998360a0cc21b51afef1781e9c0?d=identicon&s=25 Daniel Higginbotham (danieldaniel)
on 2006-05-21 05:01
(Received via mailing list)
Hello everyone,

In my model, I have

	def leave_at_formatted
		leave_at.strftime("%I:%M %p")
	end

I was wondering if there were any way to define to_s for leave_at in
order
to get the same result?

Thanks!
43ec5998360a0cc21b51afef1781e9c0?d=identicon&s=25 Daniel Higginbotham (danieldaniel)
on 2006-05-21 05:08
(Received via mailing list)
I'm afraid I may have been unclear - in the code below, leave_at is an
attribute of the model

>>
Hello everyone,

In my model, I have

	def leave_at_formatted
		leave_at.strftime("%I:%M %p")
	end

I was wondering if there were any way to define to_s for leave_at in
order
to get the same result?

Thanks!

_______________________________________________
Rails mailing list
Rails@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-05-21 06:30
(Received via mailing list)
On Sat, 2006-05-20 at 17:06 -1000, Daniel Higginbotham wrote:
> 	end
>
> I was wondering if there were any way to define to_s for leave_at in order
> to get the same result?
----
I'm gathering that 'leave_at' is actually a column in your db and you
don't want to have to convert in/out when you use it.

I have the following in my 'environment.rb'

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
  :default => '%m/%d/%Y',
  :date_time12  => "%m/%d/%Y %I:%M%p",
  :date_time24  => "%m/%d/%Y %H:%M")

but that becomes a default for all usage in my application - though this
is Date Class, I am sure that you can do similarly for Time Class.

Craig
675475d0b65710be6d992eb5eb2c61c2?d=identicon&s=25 Gregory Seidman (Guest)
on 2006-05-21 06:36
(Received via mailing list)
On Sat, May 20, 2006 at 05:06:40PM -1000, Daniel Higginbotham wrote:
} I'm afraid I may have been unclear - in the code below, leave_at is an
} attribute of the model
}
} >>
} Hello everyone,
}
} In my model, I have
}
} 	def leave_at_formatted
} 		leave_at.strftime("%I:%M %p")
} 	end
}
} I was wondering if there were any way to define to_s for leave_at in
order
} to get the same result?

I don't recommend it, but it *is* possible:

module LeaveAtFormat
  def to_s
    strftime("%I:%M %p")
  end
end

class WhateverModel
  def leave_at
    val = self[:leave_at]
    class << val
      include LeaveAtFormat
    end
    return val
  end
end

You extend the individual object to define #to_s the way you want as you
are returning it.

} Thanks!
--Greg
43ec5998360a0cc21b51afef1781e9c0?d=identicon&s=25 Daniel Higginbotham (danieldaniel)
on 2006-05-21 20:38
(Received via mailing list)
I was afraid it'd be something like that... Thanks!
43ec5998360a0cc21b51afef1781e9c0?d=identicon&s=25 Daniel Higginbotham (danieldaniel)
on 2006-05-21 20:39
(Received via mailing list)
Ah, didn't know you could do that, thanks!
This topic is locked and can not be replied to.