Hey all, I''m doing this query: @brands=Brand.find(:all,:include=>:machines,:conditions=>[''category_id=?'',cat.id]) I need to get all the brands name for each category, I get it through my machines which belong to a category and to a brand. The problem is that I get double entries in my @brands hash. I tried @brands.uniq but this is obviously not working as there are other values than name in my @brands hash. Any idea how I could remove all the doubles from my @brands? thanx in advance Pat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-15 03:57 UTC
Re: how to select distinct with an include?
Hi -- On Fri, 15 Dec 2006, Patrick Aljord wrote:> > Hey all, > I''m doing this query: > > @brands=Brand.find(:all,:include=>:machines,:conditions=>[''category_id=?'',cat.id]) > > > I need to get all the brands name for each category, I get it through > my machines which belong to a category and to a brand. The problem is > that I get double entries in my @brands hash. I tried @brands.uniq but > this is obviously not working as there are other values than name in > my @brands hash. Any idea how I could remove all the doubles from my > @brands?What does the generated SQL look like? I''m not quite picturing where the doubles are coming from. David -- Q. What''s a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.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?hl=en -~----------~----~----~----~------~----~------~--~---
Patrick Aljord wrote:> Hey all, > I''m doing this query: > > @brands=Brand.find(:all,:include=>:machines,:conditions=>[''category_id=?'',cat.id]) > > > I need to get all the brands name for each category, I get it through > my machines which belong to a category and to a brand. The problem is > that I get double entries in my @brands hash. I tried @brands.uniq but > this is obviously not working as there are other values than name in > my @brands hash. Any idea how I could remove all the doubles from my > @brands? > > thanx in advance > > PatBrand.rb has_many :machines has_many :categories, :through => machines Category.rb has_many :machines has_many :brands, :through => machines Machine.rb belongs_to :brand belongs_to :category then you can write : @all_brand_names = Category.find(:all).brands.name @category_names = Brand.find(:all).categories.name .... and much more see my cheat sheet... no, seems that posting attachment is not well accepted contact --> kerforn56 at wanadoo.fr , I''ll send it to u -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
> What does the generated SQL look like? I''m not quite picturing where > the doubles are coming from. > > > Davidok for each category I want to get all the brands it has, here is what the query is for Category.id=50: SELECT brands.`id` AS t0_r0, brands.`name` AS t0_r1, machines.`id` AS t1_r0, machines.`category_id` AS t1_r1, machines.`brand_id` AS t1_r2, machines.`model` AS t1_r3, machines.`serial` AS t1_r4, machines.`type` AS t1_r5, machines.`year` AS t1_r6, machines.`hours` AS t1_r7, machines.`miles` AS t1_r8, machines.`capacity` AS t1_r9, machines.`price` AS t1_r10, machines.`description_fr` AS t1_r11, machines.`description_en` AS t1_r12, machines.`description_es` AS t1_r13, machines.`description_ar` AS t1_r14, machines.`added_date` AS t1_r15, machines.`update_date` AS t1_r16 FROM brands LEFT OUTER JOIN machines ON machines.brand_id = brands.id WHERE (category_id=50) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
ok thanx a lot, it worked that way. On 12/15/06, Kad Kerforn <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Patrick Aljord wrote: > > Hey all, > > I''m doing this query: > > > > @brands=Brand.find(:all,:include=>:machines,:conditions=>[''category_id=?'',cat.id]) > > > > > > I need to get all the brands name for each category, I get it through > > my machines which belong to a category and to a brand. The problem is > > that I get double entries in my @brands hash. I tried @brands.uniq but > > this is obviously not working as there are other values than name in > > my @brands hash. Any idea how I could remove all the doubles from my > > @brands? > > > > thanx in advance > > > > Pat > > Brand.rb > has_many :machines > has_many :categories, :through => machines > > Category.rb > has_many :machines > has_many :brands, :through => machines > > Machine.rb > belongs_to :brand > belongs_to :category > > then you can write : > @all_brand_names = Category.find(:all).brands.name > @category_names = Brand.find(:all).categories.name > .... and much more > > see my cheat sheet... no, seems that posting attachment is not well > accepted > contact --> kerforn56 at wanadoo.fr , I''ll send it to u > > -- > 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?hl=en -~----------~----~----~----~------~----~------~--~---
On 12/15/06, Patrick Aljord <patcito-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ok thanx a lot, it worked that way. >no wait I still get double entries :( this what my query looks like: SELECT brands.* FROM brands INNER JOIN machines ON brands.id machines.brand_id WHERE (machines.category_id = 20) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-15 14:38 UTC
Re: how to select distinct with an include?
Hi -- On Fri, 15 Dec 2006, Patrick Aljord wrote:> >> What does the generated SQL look like? I''m not quite picturing where >> the doubles are coming from. >> > > ok for each category I want to get all the brands it has, here is what > the query is for Category.id=50: > > SELECT brands.`id` AS t0_r0, brands.`name` AS t0_r1, machines.`id` AS > t1_r0, machines.`category_id` AS t1_r1, machines.`brand_id` AS t1_r2, > machines.`model` AS t1_r3, machines.`serial` AS t1_r4, machines.`type` > AS t1_r5, machines.`year` AS t1_r6, machines.`hours` AS t1_r7, > machines.`miles` AS t1_r8, machines.`capacity` AS t1_r9, > machines.`price` AS t1_r10, machines.`description_fr` AS t1_r11, > machines.`description_en` AS t1_r12, machines.`description_es` AS > t1_r13, machines.`description_ar` AS t1_r14, machines.`added_date` AS > t1_r15, machines.`update_date` AS t1_r16 FROM brands LEFT OUTER JOIN > machines ON machines.brand_id = brands.id WHERE (category_id=50)Doesn''t AR squish that down into one brand with multiple eagerly-loaded machines? I''ve tried to do a mini-replica of your domain model (using household appliances, which may or may not be your actual domain :-) On the SQL side: mysql> SELECT brands.`id` AS t0_r0, brands.`name` AS t0_r1, machines.`id` AS t1_r0, machines.`name` AS t1_r1, machines.`brand_id` AS t1_r2, machines.`category_id` AS t1_r3 FROM brands LEFT OUTER JOIN machines ON machines.brand_id = brands.id WHERE (category_id = 1); +-------+-------+-------+-------------+-------+-------+ | t0_r0 | t0_r1 | t1_r0 | t1_r1 | t1_r2 | t1_r3 | +-------+-------+-------+-------------+-------+-------+ | 2 | GE | 3 | GE Washer 1 | 2 | 1 | | 2 | GE | 4 | GE Washer 2 | 2 | 1 | +-------+-------+-------+-------------+-------+-------+ 2 rows in set (0.00 sec) but on the AR side (this is what generated that SQL): >> Brand.find(:all, :include => "machines", :conditions => "category_id = 1") => [#<Brand:0xb75f8aa4 @attributes={"name"=>"GE", "id"=>"2"}, @machines=[#<Machine:0xb75f87ac @attributes={"name"=>"GE Washer 1", "brand_id"=>"2", "id"=>"3", "category_id"=>"1"}>, #<Machine:0xb75f84f0 @attributes={"name"=>"GE Washer 2", "brand_id"=>"2", "id"=>"4", "category_id"=>"1"}>]>] there''s just one Brand in the results. So I guess something is different in my replica, but I''m not sure what. (Ultimately, by the way, I imagine you could wrap this in a has_many :through construct; but of course one wants to know why it''s not working.) David -- Q. What''s a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.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?hl=en -~----------~----~----~----~------~----~------~--~---
Patrick :> no wait I still get double entries :( > this what my query looks like: > SELECT brands.* FROM brands INNER JOIN machines ON brands.id > machines.brand_id WHERE (machines.category_id = 20)- Have you tried this ? Brand.find(:all, :select => 'DISTINCT brands.*', :include => "machines", :conditions => "category_id = 1") - With has_many :through in Rails 1.2RC1, you could use :uniq : class Category < AR::B has_many :machines has_many :brands, :through => machines, :uniq => true end -- Jean-François. -- À la renverse. --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ok I did a quick hack: def getbrands(cat) @brands = Category.find(cat.id).brands ar=[] for b in @brands ar.push b.name end return ar.uniq end not perfect but it does the trick --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Dec-15 15:19 UTC
Re: how to select distinct with an include?
Hi -- On Fri, 15 Dec 2006, Patrick Aljord wrote:> > ok I did a quick hack: > def getbrands(cat) > @brands = Category.find(cat.id).brands > ar=[] > for b in @brands > ar.push b.name > end > return ar.uniq > endYou could write that as: def getbrands(cat) @brands = Category.find(cat.id).brands @brands.map {|b| b.name }.uniq end and if you don''t need the instance variable you can factor it out (or make it local). However...> not perfect but it does the trickI can''t help wondering whether there''s something engineered wrongly that is going to show itself later. I''d still be interested in trying to track it down, unless you''ve moved on to other things :-) David -- Q. What''s a good holiday present for the serious Rails developer? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) aka The Ruby book for Rails developers! Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.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?hl=en -~----------~----~----~----~------~----~------~--~---
Hi, I''m having some trouble with dates in ruby. I''d like to create a date 4 weeks ago from the current date, and find the date of the monday for that week. What is the simplest way to do this? Thanks in advance, Chris. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
t = 4.weeks.ago is a start. what you consider the monday of that week depends in part on how you define when a week starts and ends. On 12/18/06, Chris H <chris-W4T7yFXR9VraRrIkyKz72Q@public.gmane.org> wrote:> > Hi, > > I''m having some trouble with dates in ruby. I''d like to create a date > 4 weeks ago from the current date, and find the date of the monday for > that week.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Tue, 2006-12-19 at 09:36 +1000, Chris H wrote:> Hi, > > I''m having some trouble with dates in ruby. I''d like to create a date > 4 weeks ago from the current date, and find the date of the monday for > that week. > > What is the simplest way to do this? > > Thanks in advance,---- I don''t always know simplest... @h = (Time.now - 4.weeks).monday.strftime("%m-%d-%Y") Craig --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
thank you!>On Tue, 2006-12-19 at 09:36 +1000, Chris H wrote: > > >>Hi, >> >>I''m having some trouble with dates in ruby. I''d like to create a date >>4 weeks ago from the current date, and find the date of the monday for >>that week. >> >>What is the simplest way to do this? >> >>Thanks in advance, >> >> >---- >I don''t always know simplest... > >@h = (Time.now - 4.weeks).monday.strftime("%m-%d-%Y") > >Craig > > >> > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
4.weeks.ago is exactly what i was looking for! thank you!>t = 4.weeks.ago > >is a start. what you consider the monday of that week depends in part >on how you define when a week starts and ends. > >On 12/18/06, Chris H <chris-W4T7yFXR9VraRrIkyKz72Q@public.gmane.org> wrote: > > >>Hi, >> >>I''m having some trouble with dates in ruby. I''d like to create a date >>4 weeks ago from the current date, and find the date of the monday for >>that week. >> >> > >> > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
is it possible to create a date from a week number, and then call .monday on that week/date?>On Tue, 2006-12-19 at 09:36 +1000, Chris H wrote: > > >>Hi, >> >>I''m having some trouble with dates in ruby. I''d like to create a date >>4 weeks ago from the current date, and find the date of the monday for >>that week. >> >>What is the simplest way to do this? >> >>Thanks in advance, >> >> >---- >I don''t always know simplest... > >@h = (Time.now - 4.weeks).monday.strftime("%m-%d-%Y") > >Craig > > >> > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I got it! date = Time.at(50.weeks).monday cheers.>is it possible to create a date from a week number, >and then call .monday on that week/date? > > > > >>On Tue, 2006-12-19 at 09:36 +1000, Chris H wrote: >> >> >> >> >>>Hi, >>> >>>I''m having some trouble with dates in ruby. I''d like to create a date >>>4 weeks ago from the current date, and find the date of the monday for >>>that week. >>> >>>What is the simplest way to do this? >>> >>>Thanks in advance, >>> >>> >>> >>> >>---- >>I don''t always know simplest... >> >>@h = (Time.now - 4.weeks).monday.strftime("%m-%d-%Y") >> >>Craig >> >> >> >> >> >> >> >> > > >> > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
actually it''s still not quite what I need. that gives me a year of 1970. how do i get week 50 of the current year?>I got it! > >date = Time.at(50.weeks).monday > >cheers. > > > >>is it possible to create a date from a week number, >>and then call .monday on that week/date? >> >> >> >> >> >> >>>On Tue, 2006-12-19 at 09:36 +1000, Chris H wrote: >>> >>> >>> >>> >>> >>> >>>>Hi, >>>> >>>>I''m having some trouble with dates in ruby. I''d like to create a date >>>>4 weeks ago from the current date, and find the date of the monday for >>>>that week. >>>> >>>>What is the simplest way to do this? >>>> >>>>Thanks in advance, >>>> >>>> >>>> >>>> >>>> >>>> >>>---- >>>I don''t always know simplest... >>> >>>@h = (Time.now - 4.weeks).monday.strftime("%m-%d-%Y") >>> >>>Craig >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >> >> >> >> >> >> > > >> > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Time.mktime(2006,01,01) + 50.weeks answers my last post. cheers.>I got it! > >date = Time.at(50.weeks).monday > >cheers. > > > >>is it possible to create a date from a week number, >>and then call .monday on that week/date? >> >> >> >> >> >> >>>On Tue, 2006-12-19 at 09:36 +1000, Chris H wrote: >>> >>> >>> >>> >>> >>> >>>>Hi, >>>> >>>>I''m having some trouble with dates in ruby. I''d like to create a date >>>>4 weeks ago from the current date, and find the date of the monday for >>>>that week. >>>> >>>>What is the simplest way to do this? >>>> >>>>Thanks in advance, >>>> >>>> >>>> >>>> >>>> >>>> >>>---- >>>I don''t always know simplest... >>> >>>@h = (Time.now - 4.weeks).monday.strftime("%m-%d-%Y") >>> >>>Craig >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >> >> >> >> >> >> > > >> > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
4.weeks.ago.beginning_of_week ? For more methods, open a console (./scripts/comsole) and try: t=Time.now t.methods.sort This works for any everything in Ruby. On Dec 19, 11:06 am, Chris H <c...-W4T7yFXR9VraRrIkyKz72Q@public.gmane.org> wrote:> is it possible to create a date from a week number, > and then call .monday on that week/date? > > >On Tue, 2006-12-19 at 09:36 +1000, Chris H wrote: > > >>Hi, > > >>I''m having some trouble with dates in ruby. I''d like to create a date > >>4 weeks ago from the current date, and find the date of the monday for > >>that week. > > >>What is the simplest way to do this? > > >>Thanks in advance, > > >---- > >I don''t always know simplest... > > >@h = (Time.now - 4.weeks).monday.strftime("%m-%d-%Y") > > >Craig--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
irb(main):028:0> (Time.now + 50.weeks).monday => Mon Dec 03 00:00:00 -0500 2007 irb(main):029:0> (Time.now - 4.weeks).monday => Mon Nov 20 00:00:00 -0500 2006 On 12/18/06, askegg <andrew.skegg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 4.weeks.ago.beginning_of_week ? > > For more methods, open a console (./scripts/comsole) and try: > t=Time.now > t.methods.sort > > This works for any everything in Ruby. > > > > On Dec 19, 11:06 am, Chris H <c...-W4T7yFXR9VraRrIkyKz72Q@public.gmane.org> wrote: > > is it possible to create a date from a week number, > > and then call .monday on that week/date? > > > > >On Tue, 2006-12-19 at 09:36 +1000, Chris H wrote: > > > > >>Hi, > > > > >>I''m having some trouble with dates in ruby. I''d like to create a date > > >>4 weeks ago from the current date, and find the date of the monday for > > >>that week. > > > > >>What is the simplest way to do this? > > > > >>Thanks in advance, > > > > >---- > > >I don''t always know simplest... > > > > >@h = (Time.now - 4.weeks).monday.strftime("%m-%d-%Y") > > > > >Craig > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Nice. Certainly more readable than my attempt. Although the central issue here is we are using today as the starting point - Chris was looking to use the start of the year. What is missing is a "Time.now.start_of_year" method. I guess it would be easy to add "start_of_day/week/month" methods as well to help with these sorts of calculations. Then: Time.mktime(2006,01,01) + 50.weeks becomes Time.now.start_of_year + 50.weeks. Slightly better.. My other thought was: Time.now.change(:month=>1, :mday=>1, :hour=>0).in(50.weeks) Not as readable, but works and essentially captures the code for "start_of_year". On Dec 19, 12:25 pm, x1 <caldri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> irb(main):028:0> (Time.now + 50.weeks).monday > => Mon Dec 03 00:00:00 -0500 2007 > irb(main):029:0> (Time.now - 4.weeks).monday > => Mon Nov 20 00:00:00 -0500 2006 > > On 12/18/06, askegg <andrew.sk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > 4.weeks.ago.beginning_of_week ? > > > For more methods, open a console (./scripts/comsole) and try: > > t=Time.now > > t.methods.sort > > > This works for any everything in Ruby. > > > On Dec 19, 11:06 am, Chris H <c...-W4T7yFXR9VraRrIkyKz72Q@public.gmane.org> wrote: > > > is it possible to create a date from a week number, > > > and then call .monday on that week/date? > > > > >On Tue, 2006-12-19 at 09:36 +1000, Chris H wrote: > > > > >>Hi, > > > > >>I''m having some trouble with dates in ruby. I''d like to create a date > > > >>4 weeks ago from the current date, and find the date of the monday for > > > >>that week. > > > > >>What is the simplest way to do this? > > > > >>Thanks in advance, > > > > >---- > > > >I don''t always know simplest... > > > > >@h = (Time.now - 4.weeks).monday.strftime("%m-%d-%Y") > > > > >Craig--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
:-) irb(main):024:0> (Time.local(Time.now.year) + 50.weeks).monday => Mon Dec 11 00:00:00 -0500 2006 irb(main):025:0> (Time.local(Time.now.year) - 4.weeks).monday => Mon Nov 28 00:00:00 -0500 2005 On 12/18/06, askegg <andrew.skegg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Nice. Certainly more readable than my attempt. > > Although the central issue here is we are using today as the starting > point - Chris was looking to use the start of the year. > What is missing is a "Time.now.start_of_year" method. I guess it would > be easy to add "start_of_day/week/month" methods as well to help with > these sorts of calculations. Then: > Time.mktime(2006,01,01) + 50.weeks > becomes > Time.now.start_of_year + 50.weeks. > > Slightly better.. > > My other thought was: > Time.now.change(:month=>1, :mday=>1, :hour=>0).in(50.weeks) > > Not as readable, but works and essentially captures the code for > "start_of_year". > > > > > On Dec 19, 12:25 pm, x1 <caldri...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > irb(main):028:0> (Time.now + 50.weeks).monday > > => Mon Dec 03 00:00:00 -0500 2007 > > irb(main):029:0> (Time.now - 4.weeks).monday > > => Mon Nov 20 00:00:00 -0500 2006 > > > > On 12/18/06, askegg <andrew.sk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > > 4.weeks.ago.beginning_of_week ? > > > > > For more methods, open a console (./scripts/comsole) and try: > > > t=Time.now > > > t.methods.sort > > > > > This works for any everything in Ruby. > > > > > On Dec 19, 11:06 am, Chris H <c...-W4T7yFXR9VraRrIkyKz72Q@public.gmane.org> wrote: > > > > is it possible to create a date from a week number, > > > > and then call .monday on that week/date? > > > > > > >On Tue, 2006-12-19 at 09:36 +1000, Chris H wrote: > > > > > > >>Hi, > > > > > > >>I''m having some trouble with dates in ruby. I''d like to create a date > > > > >>4 weeks ago from the current date, and find the date of the monday for > > > > >>that week. > > > > > > >>What is the simplest way to do this? > > > > > > >>Thanks in advance, > > > > > > >---- > > > > >I don''t always know simplest... > > > > > > >@h = (Time.now - 4.weeks).monday.strftime("%m-%d-%Y") > > > > > > >Craig > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---