Sam Woodard
2006-Oct-23 01:11 UTC
Model class objects that are not columns in the database
I have a model Day with table days and column date, which holds a ruby and mysql date object respectively. How do I create a model object, date.year, without creating a column ''year'' in the data table, days? How do I create this global class variable in the model from an encasulated data field? Will this give me the dynamic find method, find_by_year? Sam -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thorsten L
2006-Oct-23 08:34 UTC
Re: Model class objects that are not columns in the database
The following code is surely not working as it is, as i cant even remember the proper format of a DATE column in mysql from the top of my head, but maybe it helps to push you in the rioght direction: def find_by_year(year) find :all, :conditions => ["date >= ''?-01-01'' AND date <''?-12-31''",year,year] end def year self.date.year end that should enable you to do something like: Model.find_by_year(2001) and SomeModelInstance.year --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---