Tan YL
2008-Sep-01 08:53 UTC
Re: Comparing between different attributes. programming noobie
Another question about .attributes Now that I am working on it the I don''t know to call out the header name of the coloumn. Btw I am looking through the ruby api but I just can''t seem to find this info, where do u learn .attribute from??? Could u also send me the link? -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick Cheung Sent: Wednesday, August 20, 2008 7:22 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Comparing between different attributes. programming noobie On 20 Aug 2008, at 11:55, Tan YL wrote:> > Thanks that was what I needed to know! > > Just another question: > How about if I need to call out the name of the column as well as > the value > it shows if that column has the minimum value. >something like model.attributes.(except or slice or reject).sort {| first, second| first[1] <=> second[1]} This will return a sorted array of key value pairs. Fred> -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick > Cheung > Sent: Wednesday, August 20, 2008 6:48 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Comparing between different attributes. > programming > noobie > > > > On 20 Aug 2008, at 11:41, tyliong wrote: > >> >> Hi currently i am have a app that checks the cheapest price between >> colums/attributes >> >> if model.attribute1 < model.attribute 2 >> >> result = model.attribute 1 >> >> else >> >> if model ..... >> result = model. att2 >> >> else >> >> if model att3 < >> >> >> is there any easier method to do this?? >> >> the attributes have different names so i don''t know use a loop and >> change the attribute via a counter. > > So your models have a number of columns and you want to find the > smallest one? > > Two possible ways: listing the good attribute names > > model.attributes.values_at(''name1'', ''name2'', ''name3).min > > Or all columns except the listed ones > > model.attributes.except(''id'', ''something'').values.min > > or all attributes matching a certain pattern > > model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min > > Fred > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-01 09:33 UTC
Re: Comparing between different attributes. programming noobie
On 1 Sep 2008, at 09:53, Tan YL wrote:> > Another question about .attributes > > Now that I am working on it the I don''t know to call out the header > name of > the coloumn. > > Btw I am looking through the ruby api but I just can''t seem to find > this > info, where do u learn .attribute from??? Could u also send me the > link?if you''re looking through the ruby api you won''t find it because you need to be looking at the rails ones. There are quite a few places which display the docs in a variety of formats, including http://api.rubyonrails.com/ Fred> > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick > Cheung > Sent: Wednesday, August 20, 2008 7:22 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Comparing between different attributes. > programming > noobie > > > > On 20 Aug 2008, at 11:55, Tan YL wrote: > >> >> Thanks that was what I needed to know! >> >> Just another question: >> How about if I need to call out the name of the column as well as >> the value >> it shows if that column has the minimum value. >> > > something like model.attributes.(except or slice or reject).sort {| > first, second| first[1] <=> second[1]} > This will return a sorted array of key value pairs. > > Fred > > >> -----Original Message----- >> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >> Cheung >> Sent: Wednesday, August 20, 2008 6:48 PM >> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> Subject: [Rails] Re: Comparing between different attributes. >> programming >> noobie >> >> >> >> On 20 Aug 2008, at 11:41, tyliong wrote: >> >>> >>> Hi currently i am have a app that checks the cheapest price between >>> colums/attributes >>> >>> if model.attribute1 < model.attribute 2 >>> >>> result = model.attribute 1 >>> >>> else >>> >>> if model ..... >>> result = model. att2 >>> >>> else >>> >>> if model att3 < >>> >>> >>> is there any easier method to do this?? >>> >>> the attributes have different names so i don''t know use a loop and >>> change the attribute via a counter. >> >> So your models have a number of columns and you want to find the >> smallest one? >> >> Two possible ways: listing the good attribute names >> >> model.attributes.values_at(''name1'', ''name2'', ''name3).min >> >> Or all columns except the listed ones >> >> model.attributes.except(''id'', ''something'').values.min >> >> or all attributes matching a certain pattern >> >> model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min >> >> Fred >> >> >> >>> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tan YL
2008-Sep-01 09:38 UTC
Re: Comparing between different attributes. programming noobie
I''m looking through the ruby api one at the moemtn. When I look for "attributes" they show me the methods but where is the .attribute version? -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick Cheung Sent: Monday, September 01, 2008 5:34 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Comparing between different attributes. programming noobie On 1 Sep 2008, at 09:53, Tan YL wrote:> > Another question about .attributes > > Now that I am working on it the I don''t know to call out the header > name of > the coloumn. > > Btw I am looking through the ruby api but I just can''t seem to find > this > info, where do u learn .attribute from??? Could u also send me the > link?if you''re looking through the ruby api you won''t find it because you need to be looking at the rails ones. There are quite a few places which display the docs in a variety of formats, including http://api.rubyonrails.com/ Fred> > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick > Cheung > Sent: Wednesday, August 20, 2008 7:22 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Comparing between different attributes. > programming > noobie > > > > On 20 Aug 2008, at 11:55, Tan YL wrote: > >> >> Thanks that was what I needed to know! >> >> Just another question: >> How about if I need to call out the name of the column as well as >> the value >> it shows if that column has the minimum value. >> > > something like model.attributes.(except or slice or reject).sort {| > first, second| first[1] <=> second[1]} > This will return a sorted array of key value pairs. > > Fred > > >> -----Original Message----- >> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >> Cheung >> Sent: Wednesday, August 20, 2008 6:48 PM >> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> Subject: [Rails] Re: Comparing between different attributes. >> programming >> noobie >> >> >> >> On 20 Aug 2008, at 11:41, tyliong wrote: >> >>> >>> Hi currently i am have a app that checks the cheapest price between >>> colums/attributes >>> >>> if model.attribute1 < model.attribute 2 >>> >>> result = model.attribute 1 >>> >>> else >>> >>> if model ..... >>> result = model. att2 >>> >>> else >>> >>> if model att3 < >>> >>> >>> is there any easier method to do this?? >>> >>> the attributes have different names so i don''t know use a loop and >>> change the attribute via a counter. >> >> So your models have a number of columns and you want to find the >> smallest one? >> >> Two possible ways: listing the good attribute names >> >> model.attributes.values_at(''name1'', ''name2'', ''name3).min >> >> Or all columns except the listed ones >> >> model.attributes.except(''id'', ''something'').values.min >> >> or all attributes matching a certain pattern >> >> model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min >> >> Fred >> >> >> >>> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-01 09:42 UTC
Re: Comparing between different attributes. programming noobie
On 1 Sep 2008, at 10:38, Tan YL wrote:> > I''m looking through the ruby api one at the moemtn. When I look for > "attributes" they show me the methods but where is the .attribute > version? >.attribute doesn''t exist Fred> -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick > Cheung > Sent: Monday, September 01, 2008 5:34 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Comparing between different attributes. > programming > noobie > > > > On 1 Sep 2008, at 09:53, Tan YL wrote: > >> >> Another question about .attributes >> >> Now that I am working on it the I don''t know to call out the header >> name of >> the coloumn. >> >> Btw I am looking through the ruby api but I just can''t seem to find >> this >> info, where do u learn .attribute from??? Could u also send me the >> link? > > if you''re looking through the ruby api you won''t find it because you > need to be looking at the rails ones. There are quite a few places > which display the docs in a variety of formats, including > http://api.rubyonrails.com/ > > Fred >> >> >> -----Original Message----- >> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >> Cheung >> Sent: Wednesday, August 20, 2008 7:22 PM >> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> Subject: [Rails] Re: Comparing between different attributes. >> programming >> noobie >> >> >> >> On 20 Aug 2008, at 11:55, Tan YL wrote: >> >>> >>> Thanks that was what I needed to know! >>> >>> Just another question: >>> How about if I need to call out the name of the column as well as >>> the value >>> it shows if that column has the minimum value. >>> >> >> something like model.attributes.(except or slice or reject).sort {| >> first, second| first[1] <=> second[1]} >> This will return a sorted array of key value pairs. >> >> Fred >> >> >>> -----Original Message----- >>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>> Cheung >>> Sent: Wednesday, August 20, 2008 6:48 PM >>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> Subject: [Rails] Re: Comparing between different attributes. >>> programming >>> noobie >>> >>> >>> >>> On 20 Aug 2008, at 11:41, tyliong wrote: >>> >>>> >>>> Hi currently i am have a app that checks the cheapest price between >>>> colums/attributes >>>> >>>> if model.attribute1 < model.attribute 2 >>>> >>>> result = model.attribute 1 >>>> >>>> else >>>> >>>> if model ..... >>>> result = model. att2 >>>> >>>> else >>>> >>>> if model att3 < >>>> >>>> >>>> is there any easier method to do this?? >>>> >>>> the attributes have different names so i don''t know use a loop and >>>> change the attribute via a counter. >>> >>> So your models have a number of columns and you want to find the >>> smallest one? >>> >>> Two possible ways: listing the good attribute names >>> >>> model.attributes.values_at(''name1'', ''name2'', ''name3).min >>> >>> Or all columns except the listed ones >>> >>> model.attributes.except(''id'', ''something'').values.min >>> >>> or all attributes matching a certain pattern >>> >>> model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min >>> >>> Fred >>> >>> >>> >>>> >> >> >> >> >>> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tan YL
2008-Sep-01 09:45 UTC
Re: Comparing between different attributes. programming noobie
So where do I learn to use the .attribute from and its syntax ? -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick Cheung Sent: Monday, September 01, 2008 5:43 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Comparing between different attributes. programming noobie On 1 Sep 2008, at 10:38, Tan YL wrote:> > I''m looking through the ruby api one at the moemtn. When I look for > "attributes" they show me the methods but where is the .attribute > version? >.attribute doesn''t exist Fred> -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick > Cheung > Sent: Monday, September 01, 2008 5:34 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Comparing between different attributes. > programming > noobie > > > > On 1 Sep 2008, at 09:53, Tan YL wrote: > >> >> Another question about .attributes >> >> Now that I am working on it the I don''t know to call out the header >> name of >> the coloumn. >> >> Btw I am looking through the ruby api but I just can''t seem to find >> this >> info, where do u learn .attribute from??? Could u also send me the >> link? > > if you''re looking through the ruby api you won''t find it because you > need to be looking at the rails ones. There are quite a few places > which display the docs in a variety of formats, including > http://api.rubyonrails.com/ > > Fred >> >> >> -----Original Message----- >> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >> Cheung >> Sent: Wednesday, August 20, 2008 7:22 PM >> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> Subject: [Rails] Re: Comparing between different attributes. >> programming >> noobie >> >> >> >> On 20 Aug 2008, at 11:55, Tan YL wrote: >> >>> >>> Thanks that was what I needed to know! >>> >>> Just another question: >>> How about if I need to call out the name of the column as well as >>> the value >>> it shows if that column has the minimum value. >>> >> >> something like model.attributes.(except or slice or reject).sort {| >> first, second| first[1] <=> second[1]} >> This will return a sorted array of key value pairs. >> >> Fred >> >> >>> -----Original Message----- >>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>> Cheung >>> Sent: Wednesday, August 20, 2008 6:48 PM >>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> Subject: [Rails] Re: Comparing between different attributes. >>> programming >>> noobie >>> >>> >>> >>> On 20 Aug 2008, at 11:41, tyliong wrote: >>> >>>> >>>> Hi currently i am have a app that checks the cheapest price between >>>> colums/attributes >>>> >>>> if model.attribute1 < model.attribute 2 >>>> >>>> result = model.attribute 1 >>>> >>>> else >>>> >>>> if model ..... >>>> result = model. att2 >>>> >>>> else >>>> >>>> if model att3 < >>>> >>>> >>>> is there any easier method to do this?? >>>> >>>> the attributes have different names so i don''t know use a loop and >>>> change the attribute via a counter. >>> >>> So your models have a number of columns and you want to find the >>> smallest one? >>> >>> Two possible ways: listing the good attribute names >>> >>> model.attributes.values_at(''name1'', ''name2'', ''name3).min >>> >>> Or all columns except the listed ones >>> >>> model.attributes.except(''id'', ''something'').values.min >>> >>> or all attributes matching a certain pattern >>> >>> model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min >>> >>> Fred >>> >>> >>> >>>> >> >> >> >> >>> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-01 09:47 UTC
Re: Comparing between different attributes. programming noobie
On 1 Sep 2008, at 10:45, Tan YL wrote:> > So where do I learn to use the .attribute from and its syntax ? >You don''t because there is no such method (unless we are talking at cross purposes) Fred> -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick > Cheung > Sent: Monday, September 01, 2008 5:43 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Comparing between different attributes. > programming > noobie > > > > On 1 Sep 2008, at 10:38, Tan YL wrote: > >> >> I''m looking through the ruby api one at the moemtn. When I look for >> "attributes" they show me the methods but where is the .attribute >> version? >> > .attribute doesn''t exist > > Fred > >> -----Original Message----- >> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >> Cheung >> Sent: Monday, September 01, 2008 5:34 PM >> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> Subject: [Rails] Re: Comparing between different attributes. >> programming >> noobie >> >> >> >> On 1 Sep 2008, at 09:53, Tan YL wrote: >> >>> >>> Another question about .attributes >>> >>> Now that I am working on it the I don''t know to call out the header >>> name of >>> the coloumn. >>> >>> Btw I am looking through the ruby api but I just can''t seem to find >>> this >>> info, where do u learn .attribute from??? Could u also send me the >>> link? >> >> if you''re looking through the ruby api you won''t find it because you >> need to be looking at the rails ones. There are quite a few places >> which display the docs in a variety of formats, including >> http://api.rubyonrails.com/ >> >> Fred >>> >>> >>> -----Original Message----- >>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>> Cheung >>> Sent: Wednesday, August 20, 2008 7:22 PM >>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> Subject: [Rails] Re: Comparing between different attributes. >>> programming >>> noobie >>> >>> >>> >>> On 20 Aug 2008, at 11:55, Tan YL wrote: >>> >>>> >>>> Thanks that was what I needed to know! >>>> >>>> Just another question: >>>> How about if I need to call out the name of the column as well as >>>> the value >>>> it shows if that column has the minimum value. >>>> >>> >>> something like model.attributes.(except or slice or reject).sort {| >>> first, second| first[1] <=> second[1]} >>> This will return a sorted array of key value pairs. >>> >>> Fred >>> >>> >>>> -----Original Message----- >>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>> Cheung >>>> Sent: Wednesday, August 20, 2008 6:48 PM >>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> Subject: [Rails] Re: Comparing between different attributes. >>>> programming >>>> noobie >>>> >>>> >>>> >>>> On 20 Aug 2008, at 11:41, tyliong wrote: >>>> >>>>> >>>>> Hi currently i am have a app that checks the cheapest price >>>>> between >>>>> colums/attributes >>>>> >>>>> if model.attribute1 < model.attribute 2 >>>>> >>>>> result = model.attribute 1 >>>>> >>>>> else >>>>> >>>>> if model ..... >>>>> result = model. att2 >>>>> >>>>> else >>>>> >>>>> if model att3 < >>>>> >>>>> >>>>> is there any easier method to do this?? >>>>> >>>>> the attributes have different names so i don''t know use a loop and >>>>> change the attribute via a counter. >>>> >>>> So your models have a number of columns and you want to find the >>>> smallest one? >>>> >>>> Two possible ways: listing the good attribute names >>>> >>>> model.attributes.values_at(''name1'', ''name2'', ''name3).min >>>> >>>> Or all columns except the listed ones >>>> >>>> model.attributes.except(''id'', ''something'').values.min >>>> >>>> or all attributes matching a certain pattern >>>> >>>> model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min >>>> >>>> Fred >>>> >>>> >>>> >>>>> >>> >>> >>> >>> >>>> >> >> >> >> >>> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tan YL
2008-Sep-01 09:51 UTC
Re: Comparing between different attributes. programming noobie
Not the method but the "attribute" in "model.attributes.value.min" (i don''t know what u call this type of object) -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick Cheung Sent: Monday, September 01, 2008 5:47 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Comparing between different attributes. programming noobie On 1 Sep 2008, at 10:45, Tan YL wrote:> > So where do I learn to use the .attribute from and its syntax ? >You don''t because there is no such method (unless we are talking at cross purposes) Fred> -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick > Cheung > Sent: Monday, September 01, 2008 5:43 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Comparing between different attributes. > programming > noobie > > > > On 1 Sep 2008, at 10:38, Tan YL wrote: > >> >> I''m looking through the ruby api one at the moemtn. When I look for >> "attributes" they show me the methods but where is the .attribute >> version? >> > .attribute doesn''t exist > > Fred > >> -----Original Message----- >> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >> Cheung >> Sent: Monday, September 01, 2008 5:34 PM >> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> Subject: [Rails] Re: Comparing between different attributes. >> programming >> noobie >> >> >> >> On 1 Sep 2008, at 09:53, Tan YL wrote: >> >>> >>> Another question about .attributes >>> >>> Now that I am working on it the I don''t know to call out the header >>> name of >>> the coloumn. >>> >>> Btw I am looking through the ruby api but I just can''t seem to find >>> this >>> info, where do u learn .attribute from??? Could u also send me the >>> link? >> >> if you''re looking through the ruby api you won''t find it because you >> need to be looking at the rails ones. There are quite a few places >> which display the docs in a variety of formats, including >> http://api.rubyonrails.com/ >> >> Fred >>> >>> >>> -----Original Message----- >>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>> Cheung >>> Sent: Wednesday, August 20, 2008 7:22 PM >>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> Subject: [Rails] Re: Comparing between different attributes. >>> programming >>> noobie >>> >>> >>> >>> On 20 Aug 2008, at 11:55, Tan YL wrote: >>> >>>> >>>> Thanks that was what I needed to know! >>>> >>>> Just another question: >>>> How about if I need to call out the name of the column as well as >>>> the value >>>> it shows if that column has the minimum value. >>>> >>> >>> something like model.attributes.(except or slice or reject).sort {| >>> first, second| first[1] <=> second[1]} >>> This will return a sorted array of key value pairs. >>> >>> Fred >>> >>> >>>> -----Original Message----- >>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>> Cheung >>>> Sent: Wednesday, August 20, 2008 6:48 PM >>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> Subject: [Rails] Re: Comparing between different attributes. >>>> programming >>>> noobie >>>> >>>> >>>> >>>> On 20 Aug 2008, at 11:41, tyliong wrote: >>>> >>>>> >>>>> Hi currently i am have a app that checks the cheapest price >>>>> between >>>>> colums/attributes >>>>> >>>>> if model.attribute1 < model.attribute 2 >>>>> >>>>> result = model.attribute 1 >>>>> >>>>> else >>>>> >>>>> if model ..... >>>>> result = model. att2 >>>>> >>>>> else >>>>> >>>>> if model att3 < >>>>> >>>>> >>>>> is there any easier method to do this?? >>>>> >>>>> the attributes have different names so i don''t know use a loop and >>>>> change the attribute via a counter. >>>> >>>> So your models have a number of columns and you want to find the >>>> smallest one? >>>> >>>> Two possible ways: listing the good attribute names >>>> >>>> model.attributes.values_at(''name1'', ''name2'', ''name3).min >>>> >>>> Or all columns except the listed ones >>>> >>>> model.attributes.except(''id'', ''something'').values.min >>>> >>>> or all attributes matching a certain pattern >>>> >>>> model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min >>>> >>>> Fred >>>> >>>> >>>> >>>>> >>> >>> >>> >>> >>>> >> >> >> >> >>> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-01 09:54 UTC
Re: Comparing between different attributes. programming noobie
On 1 Sep 2008, at 10:51, Tan YL wrote:> > Not the method but the "attribute" in > "model.attributes.value.min" (i don''t > know what u call this type of object)Sorry, but I''ve no idea what you''re going on about. Fred> > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick > Cheung > Sent: Monday, September 01, 2008 5:47 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Comparing between different attributes. > programming > noobie > > > > On 1 Sep 2008, at 10:45, Tan YL wrote: > >> >> So where do I learn to use the .attribute from and its syntax ? >> > You don''t because there is no such method (unless we are talking at > cross purposes) > > Fred > >> -----Original Message----- >> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >> Cheung >> Sent: Monday, September 01, 2008 5:43 PM >> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> Subject: [Rails] Re: Comparing between different attributes. >> programming >> noobie >> >> >> >> On 1 Sep 2008, at 10:38, Tan YL wrote: >> >>> >>> I''m looking through the ruby api one at the moemtn. When I look for >>> "attributes" they show me the methods but where is the .attribute >>> version? >>> >> .attribute doesn''t exist >> >> Fred >> >>> -----Original Message----- >>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>> Cheung >>> Sent: Monday, September 01, 2008 5:34 PM >>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> Subject: [Rails] Re: Comparing between different attributes. >>> programming >>> noobie >>> >>> >>> >>> On 1 Sep 2008, at 09:53, Tan YL wrote: >>> >>>> >>>> Another question about .attributes >>>> >>>> Now that I am working on it the I don''t know to call out the header >>>> name of >>>> the coloumn. >>>> >>>> Btw I am looking through the ruby api but I just can''t seem to find >>>> this >>>> info, where do u learn .attribute from??? Could u also send me the >>>> link? >>> >>> if you''re looking through the ruby api you won''t find it because you >>> need to be looking at the rails ones. There are quite a few places >>> which display the docs in a variety of formats, including >>> http://api.rubyonrails.com/ >>> >>> Fred >>>> >>>> >>>> -----Original Message----- >>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>> Cheung >>>> Sent: Wednesday, August 20, 2008 7:22 PM >>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> Subject: [Rails] Re: Comparing between different attributes. >>>> programming >>>> noobie >>>> >>>> >>>> >>>> On 20 Aug 2008, at 11:55, Tan YL wrote: >>>> >>>>> >>>>> Thanks that was what I needed to know! >>>>> >>>>> Just another question: >>>>> How about if I need to call out the name of the column as well as >>>>> the value >>>>> it shows if that column has the minimum value. >>>>> >>>> >>>> something like model.attributes.(except or slice or reject).sort {| >>>> first, second| first[1] <=> second[1]} >>>> This will return a sorted array of key value pairs. >>>> >>>> Fred >>>> >>>> >>>>> -----Original Message----- >>>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>>> Cheung >>>>> Sent: Wednesday, August 20, 2008 6:48 PM >>>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>> Subject: [Rails] Re: Comparing between different attributes. >>>>> programming >>>>> noobie >>>>> >>>>> >>>>> >>>>> On 20 Aug 2008, at 11:41, tyliong wrote: >>>>> >>>>>> >>>>>> Hi currently i am have a app that checks the cheapest price >>>>>> between >>>>>> colums/attributes >>>>>> >>>>>> if model.attribute1 < model.attribute 2 >>>>>> >>>>>> result = model.attribute 1 >>>>>> >>>>>> else >>>>>> >>>>>> if model ..... >>>>>> result = model. att2 >>>>>> >>>>>> else >>>>>> >>>>>> if model att3 < >>>>>> >>>>>> >>>>>> is there any easier method to do this?? >>>>>> >>>>>> the attributes have different names so i don''t know use a loop >>>>>> and >>>>>> change the attribute via a counter. >>>>> >>>>> So your models have a number of columns and you want to find the >>>>> smallest one? >>>>> >>>>> Two possible ways: listing the good attribute names >>>>> >>>>> model.attributes.values_at(''name1'', ''name2'', ''name3).min >>>>> >>>>> Or all columns except the listed ones >>>>> >>>>> model.attributes.except(''id'', ''something'').values.min >>>>> >>>>> or all attributes matching a certain pattern >>>>> >>>>> model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min >>>>> >>>>> Fred >>>>> >>>>> >>>>> >>>>>> >>>> >>>> >>>> >>>> >>>>> >>> >>> >>> >>> >>>> >> >> >> >> >>> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tan YL
2008-Sep-01 10:01 UTC
Re: Comparing between different attributes. programming noobie
Let me try to rephrase: where do u learn the how to manipulate the model with those extensions. For instance (model. "attribute". "value") where do u learn that u can add those values to the model so that u can bring something like the attribute or the cell value of the record out I can''t seem to find any resource on it. The api gives methods but not this(or maybe I can''t seem to find where it is in the api) -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick Cheung Sent: Monday, September 01, 2008 5:55 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Comparing between different attributes. programming noobie On 1 Sep 2008, at 10:51, Tan YL wrote:> > Not the method but the "attribute" in > "model.attributes.value.min" (i don''t > know what u call this type of object)Sorry, but I''ve no idea what you''re going on about. Fred> > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick > Cheung > Sent: Monday, September 01, 2008 5:47 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Comparing between different attributes. > programming > noobie > > > > On 1 Sep 2008, at 10:45, Tan YL wrote: > >> >> So where do I learn to use the .attribute from and its syntax ? >> > You don''t because there is no such method (unless we are talking at > cross purposes) > > Fred > >> -----Original Message----- >> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >> Cheung >> Sent: Monday, September 01, 2008 5:43 PM >> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> Subject: [Rails] Re: Comparing between different attributes. >> programming >> noobie >> >> >> >> On 1 Sep 2008, at 10:38, Tan YL wrote: >> >>> >>> I''m looking through the ruby api one at the moemtn. When I look for >>> "attributes" they show me the methods but where is the .attribute >>> version? >>> >> .attribute doesn''t exist >> >> Fred >> >>> -----Original Message----- >>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>> Cheung >>> Sent: Monday, September 01, 2008 5:34 PM >>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> Subject: [Rails] Re: Comparing between different attributes. >>> programming >>> noobie >>> >>> >>> >>> On 1 Sep 2008, at 09:53, Tan YL wrote: >>> >>>> >>>> Another question about .attributes >>>> >>>> Now that I am working on it the I don''t know to call out the header >>>> name of >>>> the coloumn. >>>> >>>> Btw I am looking through the ruby api but I just can''t seem to find >>>> this >>>> info, where do u learn .attribute from??? Could u also send me the >>>> link? >>> >>> if you''re looking through the ruby api you won''t find it because you >>> need to be looking at the rails ones. There are quite a few places >>> which display the docs in a variety of formats, including >>> http://api.rubyonrails.com/ >>> >>> Fred >>>> >>>> >>>> -----Original Message----- >>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>> Cheung >>>> Sent: Wednesday, August 20, 2008 7:22 PM >>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> Subject: [Rails] Re: Comparing between different attributes. >>>> programming >>>> noobie >>>> >>>> >>>> >>>> On 20 Aug 2008, at 11:55, Tan YL wrote: >>>> >>>>> >>>>> Thanks that was what I needed to know! >>>>> >>>>> Just another question: >>>>> How about if I need to call out the name of the column as well as >>>>> the value >>>>> it shows if that column has the minimum value. >>>>> >>>> >>>> something like model.attributes.(except or slice or reject).sort {| >>>> first, second| first[1] <=> second[1]} >>>> This will return a sorted array of key value pairs. >>>> >>>> Fred >>>> >>>> >>>>> -----Original Message----- >>>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>>> Cheung >>>>> Sent: Wednesday, August 20, 2008 6:48 PM >>>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>> Subject: [Rails] Re: Comparing between different attributes. >>>>> programming >>>>> noobie >>>>> >>>>> >>>>> >>>>> On 20 Aug 2008, at 11:41, tyliong wrote: >>>>> >>>>>> >>>>>> Hi currently i am have a app that checks the cheapest price >>>>>> between >>>>>> colums/attributes >>>>>> >>>>>> if model.attribute1 < model.attribute 2 >>>>>> >>>>>> result = model.attribute 1 >>>>>> >>>>>> else >>>>>> >>>>>> if model ..... >>>>>> result = model. att2 >>>>>> >>>>>> else >>>>>> >>>>>> if model att3 < >>>>>> >>>>>> >>>>>> is there any easier method to do this?? >>>>>> >>>>>> the attributes have different names so i don''t know use a loop >>>>>> and >>>>>> change the attribute via a counter. >>>>> >>>>> So your models have a number of columns and you want to find the >>>>> smallest one? >>>>> >>>>> Two possible ways: listing the good attribute names >>>>> >>>>> model.attributes.values_at(''name1'', ''name2'', ''name3).min >>>>> >>>>> Or all columns except the listed ones >>>>> >>>>> model.attributes.except(''id'', ''something'').values.min >>>>> >>>>> or all attributes matching a certain pattern >>>>> >>>>> model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min >>>>> >>>>> Fred >>>>> >>>>> >>>>> >>>>>> >>>> >>>> >>>> >>>> >>>>> >>> >>> >>> >>> >>>> >> >> >> >> >>> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Sep-01 10:08 UTC
Re: Comparing between different attributes. programming noobie
On 1 Sep 2008, at 11:01, Tan YL wrote:> > Let me try to rephrase: where do u learn the how to manipulate the > model > with those extensions. For instance (model. "attribute". "value") > where do u > learn that u can add those values to the model so that u can bring > something > like the attribute or the cell value of the record out I can''t seem > to find > any resource on it. The api gives methods but not this(or maybe I > can''t seem > to find where it is in the api)if you have an instance of a model, some of the methods are part of the api, but some are added at runtime based on information from the database. So for example if your users table has a name column then instances of User will have a name method, but there is nowhere you can look that up. There are a few other places where rails generates methods at run time (eg dynamic finders). Fred> > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick > Cheung > Sent: Monday, September 01, 2008 5:55 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Comparing between different attributes. > programming > noobie > > > > On 1 Sep 2008, at 10:51, Tan YL wrote: > >> >> Not the method but the "attribute" in >> "model.attributes.value.min" (i don''t >> know what u call this type of object) > > Sorry, but I''ve no idea what you''re going on about. > > Fred > >> >> >> -----Original Message----- >> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >> Cheung >> Sent: Monday, September 01, 2008 5:47 PM >> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> Subject: [Rails] Re: Comparing between different attributes. >> programming >> noobie >> >> >> >> On 1 Sep 2008, at 10:45, Tan YL wrote: >> >>> >>> So where do I learn to use the .attribute from and its syntax ? >>> >> You don''t because there is no such method (unless we are talking at >> cross purposes) >> >> Fred >> >>> -----Original Message----- >>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>> Cheung >>> Sent: Monday, September 01, 2008 5:43 PM >>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> Subject: [Rails] Re: Comparing between different attributes. >>> programming >>> noobie >>> >>> >>> >>> On 1 Sep 2008, at 10:38, Tan YL wrote: >>> >>>> >>>> I''m looking through the ruby api one at the moemtn. When I look for >>>> "attributes" they show me the methods but where is the .attribute >>>> version? >>>> >>> .attribute doesn''t exist >>> >>> Fred >>> >>>> -----Original Message----- >>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>> Cheung >>>> Sent: Monday, September 01, 2008 5:34 PM >>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> Subject: [Rails] Re: Comparing between different attributes. >>>> programming >>>> noobie >>>> >>>> >>>> >>>> On 1 Sep 2008, at 09:53, Tan YL wrote: >>>> >>>>> >>>>> Another question about .attributes >>>>> >>>>> Now that I am working on it the I don''t know to call out the >>>>> header >>>>> name of >>>>> the coloumn. >>>>> >>>>> Btw I am looking through the ruby api but I just can''t seem to >>>>> find >>>>> this >>>>> info, where do u learn .attribute from??? Could u also send me the >>>>> link? >>>> >>>> if you''re looking through the ruby api you won''t find it because >>>> you >>>> need to be looking at the rails ones. There are quite a few places >>>> which display the docs in a variety of formats, including >>>> http://api.rubyonrails.com/ >>>> >>>> Fred >>>>> >>>>> >>>>> -----Original Message----- >>>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>>> Cheung >>>>> Sent: Wednesday, August 20, 2008 7:22 PM >>>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>> Subject: [Rails] Re: Comparing between different attributes. >>>>> programming >>>>> noobie >>>>> >>>>> >>>>> >>>>> On 20 Aug 2008, at 11:55, Tan YL wrote: >>>>> >>>>>> >>>>>> Thanks that was what I needed to know! >>>>>> >>>>>> Just another question: >>>>>> How about if I need to call out the name of the column as well as >>>>>> the value >>>>>> it shows if that column has the minimum value. >>>>>> >>>>> >>>>> something like model.attributes.(except or slice or reject).sort >>>>> {| >>>>> first, second| first[1] <=> second[1]} >>>>> This will return a sorted array of key value pairs. >>>>> >>>>> Fred >>>>> >>>>> >>>>>> -----Original Message----- >>>>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>>>> Cheung >>>>>> Sent: Wednesday, August 20, 2008 6:48 PM >>>>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>>> Subject: [Rails] Re: Comparing between different attributes. >>>>>> programming >>>>>> noobie >>>>>> >>>>>> >>>>>> >>>>>> On 20 Aug 2008, at 11:41, tyliong wrote: >>>>>> >>>>>>> >>>>>>> Hi currently i am have a app that checks the cheapest price >>>>>>> between >>>>>>> colums/attributes >>>>>>> >>>>>>> if model.attribute1 < model.attribute 2 >>>>>>> >>>>>>> result = model.attribute 1 >>>>>>> >>>>>>> else >>>>>>> >>>>>>> if model ..... >>>>>>> result = model. att2 >>>>>>> >>>>>>> else >>>>>>> >>>>>>> if model att3 < >>>>>>> >>>>>>> >>>>>>> is there any easier method to do this?? >>>>>>> >>>>>>> the attributes have different names so i don''t know use a loop >>>>>>> and >>>>>>> change the attribute via a counter. >>>>>> >>>>>> So your models have a number of columns and you want to find the >>>>>> smallest one? >>>>>> >>>>>> Two possible ways: listing the good attribute names >>>>>> >>>>>> model.attributes.values_at(''name1'', ''name2'', ''name3).min >>>>>> >>>>>> Or all columns except the listed ones >>>>>> >>>>>> model.attributes.except(''id'', ''something'').values.min >>>>>> >>>>>> or all attributes matching a certain pattern >>>>>> >>>>>> model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min >>>>>> >>>>>> Fred >>>>>> >>>>>> >>>>>> >>>>>>> >>>>> >>>>> >>>>> >>>>> >>>>>> >>>> >>>> >>>> >>>> >>>>> >>> >>> >>> >>> >>>> >> >> >> >> >>> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Tan YL
2008-Sep-01 11:05 UTC
Re: Comparing between different attributes. programming noobie
Thanks for your help. -----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick Cheung Sent: Monday, September 01, 2008 6:09 PM To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Subject: [Rails] Re: Comparing between different attributes. programming noobie On 1 Sep 2008, at 11:01, Tan YL wrote:> > Let me try to rephrase: where do u learn the how to manipulate the > model > with those extensions. For instance (model. "attribute". "value") > where do u > learn that u can add those values to the model so that u can bring > something > like the attribute or the cell value of the record out I can''t seem > to find > any resource on it. The api gives methods but not this(or maybe I > can''t seem > to find where it is in the api)if you have an instance of a model, some of the methods are part of the api, but some are added at runtime based on information from the database. So for example if your users table has a name column then instances of User will have a name method, but there is nowhere you can look that up. There are a few other places where rails generates methods at run time (eg dynamic finders). Fred> > > -----Original Message----- > From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick > Cheung > Sent: Monday, September 01, 2008 5:55 PM > To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > Subject: [Rails] Re: Comparing between different attributes. > programming > noobie > > > > On 1 Sep 2008, at 10:51, Tan YL wrote: > >> >> Not the method but the "attribute" in >> "model.attributes.value.min" (i don''t >> know what u call this type of object) > > Sorry, but I''ve no idea what you''re going on about. > > Fred > >> >> >> -----Original Message----- >> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >> Cheung >> Sent: Monday, September 01, 2008 5:47 PM >> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >> Subject: [Rails] Re: Comparing between different attributes. >> programming >> noobie >> >> >> >> On 1 Sep 2008, at 10:45, Tan YL wrote: >> >>> >>> So where do I learn to use the .attribute from and its syntax ? >>> >> You don''t because there is no such method (unless we are talking at >> cross purposes) >> >> Fred >> >>> -----Original Message----- >>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>> Cheung >>> Sent: Monday, September 01, 2008 5:43 PM >>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>> Subject: [Rails] Re: Comparing between different attributes. >>> programming >>> noobie >>> >>> >>> >>> On 1 Sep 2008, at 10:38, Tan YL wrote: >>> >>>> >>>> I''m looking through the ruby api one at the moemtn. When I look for >>>> "attributes" they show me the methods but where is the .attribute >>>> version? >>>> >>> .attribute doesn''t exist >>> >>> Fred >>> >>>> -----Original Message----- >>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>> Cheung >>>> Sent: Monday, September 01, 2008 5:34 PM >>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>> Subject: [Rails] Re: Comparing between different attributes. >>>> programming >>>> noobie >>>> >>>> >>>> >>>> On 1 Sep 2008, at 09:53, Tan YL wrote: >>>> >>>>> >>>>> Another question about .attributes >>>>> >>>>> Now that I am working on it the I don''t know to call out the >>>>> header >>>>> name of >>>>> the coloumn. >>>>> >>>>> Btw I am looking through the ruby api but I just can''t seem to >>>>> find >>>>> this >>>>> info, where do u learn .attribute from??? Could u also send me the >>>>> link? >>>> >>>> if you''re looking through the ruby api you won''t find it because >>>> you >>>> need to be looking at the rails ones. There are quite a few places >>>> which display the docs in a variety of formats, including >>>> http://api.rubyonrails.com/ >>>> >>>> Fred >>>>> >>>>> >>>>> -----Original Message----- >>>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>>> Cheung >>>>> Sent: Wednesday, August 20, 2008 7:22 PM >>>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>> Subject: [Rails] Re: Comparing between different attributes. >>>>> programming >>>>> noobie >>>>> >>>>> >>>>> >>>>> On 20 Aug 2008, at 11:55, Tan YL wrote: >>>>> >>>>>> >>>>>> Thanks that was what I needed to know! >>>>>> >>>>>> Just another question: >>>>>> How about if I need to call out the name of the column as well as >>>>>> the value >>>>>> it shows if that column has the minimum value. >>>>>> >>>>> >>>>> something like model.attributes.(except or slice or reject).sort >>>>> {| >>>>> first, second| first[1] <=> second[1]} >>>>> This will return a sorted array of key value pairs. >>>>> >>>>> Fred >>>>> >>>>> >>>>>> -----Original Message----- >>>>>> From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>>> [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Frederick >>>>>> Cheung >>>>>> Sent: Wednesday, August 20, 2008 6:48 PM >>>>>> To: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org >>>>>> Subject: [Rails] Re: Comparing between different attributes. >>>>>> programming >>>>>> noobie >>>>>> >>>>>> >>>>>> >>>>>> On 20 Aug 2008, at 11:41, tyliong wrote: >>>>>> >>>>>>> >>>>>>> Hi currently i am have a app that checks the cheapest price >>>>>>> between >>>>>>> colums/attributes >>>>>>> >>>>>>> if model.attribute1 < model.attribute 2 >>>>>>> >>>>>>> result = model.attribute 1 >>>>>>> >>>>>>> else >>>>>>> >>>>>>> if model ..... >>>>>>> result = model. att2 >>>>>>> >>>>>>> else >>>>>>> >>>>>>> if model att3 < >>>>>>> >>>>>>> >>>>>>> is there any easier method to do this?? >>>>>>> >>>>>>> the attributes have different names so i don''t know use a loop >>>>>>> and >>>>>>> change the attribute via a counter. >>>>>> >>>>>> So your models have a number of columns and you want to find the >>>>>> smallest one? >>>>>> >>>>>> Two possible ways: listing the good attribute names >>>>>> >>>>>> model.attributes.values_at(''name1'', ''name2'', ''name3).min >>>>>> >>>>>> Or all columns except the listed ones >>>>>> >>>>>> model.attributes.except(''id'', ''something'').values.min >>>>>> >>>>>> or all attributes matching a certain pattern >>>>>> >>>>>> model.attributes.reject {|k, v| k !~ /some_pattern/}.values.min >>>>>> >>>>>> Fred >>>>>> >>>>>> >>>>>> >>>>>>> >>>>> >>>>> >>>>> >>>>> >>>>>> >>>> >>>> >>>> >>>> >>>>> >>> >>> >>> >>> >>>> >> >> >> >> >>> > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---