Ben Stovold
2005-May-16 22:55 UTC
Noob: can I get a list of unique attributes with ActiveRecord?
Railers, I have a simple blog app with a model for ‘Article’ that has a ‘created’ column in sqlite (datetime). I’d like to build a helper that gives me a collection of “month” objects representing the months for which articles exist, each with a 3 attributes: a text ‘description’ of “month-year”, and the ‘month’ and ‘year’ numbers I need to construct the appropriate url for each. I can do strftime, my prob is figuring out how to pull back the list of months. I don’t really want to have to do Article.find :all then try and parse out the months with Ruby (not exactly efficient on the db). Is there an easy way to do this in ActiveRecord? Thanks in advance for the the tips. -- No virus found in this outgoing message. Checked by AVG Anti-Virus. Version: 7.0.308 / Virus Database: 266.11.9 - Release Date: 12/05/2005 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jarkko Laine
2005-May-17 07:52 UTC
Re: Noob: can I get a list of unique attributes with ActiveRecord?
On 17.5.2005, at 01:55, Ben Stovold wrote:> Railers, > > I have a simple blog app with a model for ‘Article’ that has a > ‘created’ column in sqlite (datetime). > > I’d like to build a helper that gives me a collection of “month” > objects representing the months for which articles exist, each with a > 3 attributes: a text ‘description’ of “month-year”, and the ‘month’ > and ‘year’ numbers I need to construct the appropriate url for each. > > I can do strftime, my prob is figuring out how to pull back the list > of months. I don’t really want to have to do Article.find :all then > try and parse out the months with Ruby (not exactly efficient on the > db). Is there an easy way to do this in ActiveRecord?I don''t think so. I would create a class method that uses select_all [1] (i.e. pure sql) to return the months: select distinct month(created_at) as month, year(created_at) as year, concat(monthname(created_at), '' '', year(created_at) ) as description from news; Make sure you have an index for these (if that''s possible in your db), otherwise the query can become pretty expensive when the size of the table grows. [1] http://rails.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/ AbstractAdapter.html#M000543 //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On The Rails
2005-May-17 10:47 UTC
Re: Noob: can I get a list of unique attributes with ActiveRecord?
Thanks Jarkko. Gotta say, seems like quite a bit of work for what strikes me as a potentially common requirement, ie. reading/manipulating values for an attribute across a model. I''m surprised there''s not an easier way to do this in Active Record. Gives me pause: am I approaching the problem the wrong way? I simply want to be able to construct unordered list of the months my articles were published, with a link to the appropriate archive page... Cheers again, Ben. On 5/17/05, Jarkko Laine <jarkko-k1O+Gnc6WpmsTnJN9+BGXg@public.gmane.org> wrote:> On 17.5.2005, at 01:55, Ben Stovold wrote: > > > Railers, > > > > I have a simple blog app with a model for ''Article'' that has a > > ''created'' column in sqlite (datetime). > > > > I''d like to build a helper that gives me a collection of "month" > > objects representing the months for which articles exist, each with a > > 3 attributes: a text ''description'' of "month-year", and the ''month'' > > and ''year'' numbers I need to construct the appropriate url for each. > > > > I can do strftime, my prob is figuring out how to pull back the list > > of months. I don''t really want to have to do Article.find :all then > > try and parse out the months with Ruby (not exactly efficient on the > > db). Is there an easy way to do this in ActiveRecord? > > I don''t think so. I would create a class method that uses select_all > [1] (i.e. pure sql) to return the months: > > select distinct month(created_at) as month, > year(created_at) as year, > concat(monthname(created_at), '' '', year(created_at) ) as description > from news; > > Make sure you have an index for these (if that''s possible in your db), > otherwise the query can become pretty expensive when the size of the > table grows. > > [1] > http://rails.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/ > AbstractAdapter.html#M000543 > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >