I have this code sample which doesnt work properly. <% @advert_types.each do |at| %> <div id="advert_type_name"> <b><%= at.name %></b> </div> <% @resort.adverts.cleared_for_release.each do |advert| %> <div id="name"> <%= render :partial => ''adverts/small_advert_for_side_panel'', :locals => { :advert => advert } %> </div> <% end %> <% end %> I''m trying to show my cleared adverts in groups (grouped by advert_type). This code gives me my groups but the advert is listed under all categories! How do I do it? -- Posted via http://www.ruby-forum.com/.
OK, following on - I did it, but I have another problem now.. here''s some code... advert.rb default_scope :order => "adverts.created_at DESC", :limit => 100 named_scope :cleared_for_release, :conditions => [''cleared_for_release = ?'', true] has_and_belongs_to_many :resorts belongs_to :advert_type advert_type.rb acts_as_list default_scope :order => ''position'' has_many :adverts, :dependent => :destroy resort show controller extract @resort = Resort.find(params[:id]) @adverts = @resort.adverts.cleared_for_release @advert_types = @adverts.group_by { |at| at.advert_type } show view extract <% @advert_types.each do |type, advert| %> <%= type.name %> <% advert.each do |advert| %> <%= render :partial => ''adverts/small_advert_for_side_panel'', :locals => { :advert => advert } %> <% end %> --------- It''s all wired up and working great, the show view spits out my adverts grouped by advert type - BUT...not the sorting of the types based on AdvertType.position! grrrrrrr. What I want is this across the App. AdvertType categories displayed on basis of AdvertType.position (an integer) Adverts sorted within AdvertTypes alphabetically by Advert.created_at ----- Heeeeeelp - :-) bb -- Posted via http://www.ruby-forum.com/.
> > show view extract > > <% @advert_types.each do |type, advert| %> > <%= type.name %> > <% advert.each do |advert| %> > <%= render :partial => ''adverts/small_advert_for_side_panel'', > :locals => { :advert => advert } %> > <% end %> >I''ve realised what I need to do here is probably something like... @advert_types.sort.each do |type, advert| That''s giving me this error though, and frankly I want the sort to be on the position column. very frustrating! -- Posted via http://www.ruby-forum.com/.
2009/9/12 bingo bob <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > >> >> show view extract >> >> <% @advert_types.each do |type, advert| %>Should that be adverts not advert?>> <%= type.name %> >> <% advert.each do |advert| %>adverts.each ?>> <%= render :partial => ''adverts/small_advert_for_side_panel'', >> :locals => { :advert => advert } %> >> <% end %> >> > > > I''ve realised what I need to do here is probably something like... > > @advert_types.sort.each do |type, advert| >Have a look at sort_by, you can do something like @advert_types.sort_by { |t| t.position } Colin
yes to the adverts in plural - my mishtake. can''t get this to work though @advert_types.sort_by { |t| t.position } mind expanding the example? -- Posted via http://www.ruby-forum.com/.
2009/9/12 bingo bob <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > yes to the adverts in plural - my mishtake. > > can''t get this to work though @advert_types.sort_by { |t| t.position } > > mind expanding the example?Not much to expand, what happens when you do it? Have you met ruby-debug? I find it invaluable when some code is not doing what you expect, you can break in at the appropriate point and examine the variables. Have a look at the rails guide on debugging. (I presume you have already worked through the getting started guide. The ActiveRecord associations one is compulsory also.) Colin
>> can''t get this to work though @advert_types.sort_by { |t| t.position }> Not much to expand, what happens when you do it?I get this... undefined method `position'' for #<Array:0x1032bb4d0> -- Posted via http://www.ruby-forum.com/.
bingo bob wrote:>>> can''t get this to work though @advert_types.sort_by { |t| t.position } > >> Not much to expand, what happens when you do it? > > I get this... > > undefined method `position'' for #<Array:0x1032bb4d0>Which figures as an array wouldn''t have a position the AdvertType does though. -- Posted via http://www.ruby-forum.com/.
2009/9/12 bingo bob <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > bingo bob wrote: >>>> can''t get this to work though @advert_types.sort_by { |t| t.position } >> >>> Not much to expand, what happens when you do it? >> >> I get this... >> >> undefined method `position'' for #<Array:0x1032bb4d0> > > Which figures as an array wouldn''t have a position the AdvertType does > though.Strange, t should be a single advert_type by the time it gets into the block. As I expect you saw from the sort_by docs each item to be sorted is passed into the block and the result used as the thing to sort by. Can you paste the line of code it is failing on, together with the surrounding ones. Use copy/paste rather than re-typing in case there is a typo in it that you have not seen. Colin
24: <% else %> 25: 26: 27: <% @advert_types.sort_by { |t| t.position } %> 28: 29: <% @advert_types.each do |type, adverts| %> 30: <div id="advert_type_name"> Showing app/views/resorts/show.html.erb where line #27 raised: undefined method `position'' for #<Array:0x103059de0> -- Posted via http://www.ruby-forum.com/.
2009/9/12 bingo bob <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > 24: <% else %> > 25: > 26: > 27: <% @advert_types.sort_by { |t| t.position } %>Firstly it would be much better to do this in the controller, or even better in a method of one of the models. Secondly sort_by does not modify the array (@advert_types in this case) it just returns another array, so you have to say @advert_types = @advert_types.sort_by ... Neither of which explains why t is an array. Are you sure that @advert_types is what you think it is, an array of AdvertTypes? Could it be something else. I suggest maybe looking at the docs for group_by and possibly call it something different. Go in with the debugger and have a look where you make advert_types. You can do things like p @advert_types[0] and see what is in it.> 28: > 29: <% @advert_types.each do |type, adverts| %>This is a bit of a clue that @advert_types is not an array of AdvertTypes. You may need to make the contents of the block in sort_by a little more subtle. Colin> 30: <div id="advert_type_name"> > > Showing app/views/resorts/show.html.erb where line #27 raised: > > undefined method `position'' for #<Array:0x103059de0> > > > > -- > Posted via http://www.ruby-forum.com/. > > > >
ok i did this... <%= p @advert_types.inspect %> and got this "#<OrderedHash {#<AdvertType id: 1, name: \"Accommodation\", description: \"Accommodation\", created_at: \"2009-08-30 10:20:38\", updated_at: \"2009-08-30 10:20:38\", position: 2>=>[#<Advert id: 6, advert_type_id:...SNIP, #<AdvertType id: 2, name: \"Bars and Restaurants\", description: \"Bars and Restaurants\", created_at: \"2009-08-30 10:20:38\", updated_at: \"2009-08-30 10:20:38\", position: 3>=>[#<Advert id: 9, advert_type_id: 2, user_id: 13, name: \"Bobs Bar\", tag_line: \"aksl daslkas kl askl sadklashkld hklas hlskahasklhd...\", body: \"aksl das...SNIP.. So yes, it''s misleading calling it advert types. It''s Advert Types and adverts within it as an order hash! How can I control the order of the outer Advert Type in this case? I guess a better name would be adverts_by_type or somesuch. -- Posted via http://www.ruby-forum.com/.
2009/9/12 bingo bob <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > > ok i did this... > <%= p @advert_types.inspect %> > > and got this > > > "#<OrderedHash {#<AdvertType id: 1, name: \"Accommodation\", > description: \"Accommodation\", created_at: \"2009-08-30 10:20:38\", > updated_at: \"2009-08-30 10:20:38\", position: 2>=>[#<Advert id: 6, > advert_type_id:...SNIP, > > #<AdvertType id: 2, name: \"Bars and Restaurants\", description: \"Bars > and Restaurants\", created_at: \"2009-08-30 10:20:38\", updated_at: > \"2009-08-30 10:20:38\", position: 3>=>[#<Advert id: 9, advert_type_id: > 2, user_id: 13, name: \"Bobs Bar\", tag_line: \"aksl daslkas kl askl > sadklashkld hklas hlskahasklhd...\", body: \"aksl das...SNIP.. > > So yes, it''s misleading calling it advert types. It''s Advert Types and > adverts within it as an order hash! > > How can I control the order of the outer Advert Type in this case? > > I guess a better name would be adverts_by_type or somesuch.Have you considered grouping on the position rather than the type, then the key will be the position itself and I think you should be able to do something like @whatever_you_want_to_call_them_sorted = @adverts.group_by { |advert| advert.advert_type.position }.sort Colin
Interesting closer i think... I''m dojng this currently...(sure I know this is better in the controller ultimately). <% @advert_types_sorted = @adverts.group_by { |advert| advert.advert_type.position }.sort %> <%= p @advert_types_sorted.inspect %> <% @advert_types_sorted.each do |type, adverts| %> <div id="advert_type_name"> <%= type.inspect %> </div> type.inspect is giving me an number value, I suspect the ID of the advert_type rather than the advert type iteslef, so I can''t currently do type.name that errors with undefined method `name'' for 2:Fixnum. close? -- Posted via http://www.ruby-forum.com/.
actually scrub that, dont think the number is the ID of the advertype -- Posted via http://www.ruby-forum.com/.
2009/9/12 bingo bob <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > > Interesting closer i think... > > I''m dojng this currently...(sure I know this is better in the controller > ultimately). > > <% @advert_types_sorted = @adverts.group_by { |advert| > advert.advert_type.position }.sort %> > <%= p @advert_types_sorted.inspect %> > > <% @advert_types_sorted.each do |type, adverts| %> > <div id="advert_type_name"> > <%= type.inspect %> > </div> > > type.inspect is giving me an number value, I suspect the ID of the > advert_type rather than the advert type iteslef, so I can''t currently do > type.name that errors with undefined method `name'' for 2:Fixnum. > > close?Since you are grouping on the position ''type'' should be the position. To get the advert type do adverts[0].advert_type.name, or do it in the adverts.each loop. Colin
Colin, You nailed it. Working great in my app - lovely. Many thanks indeed for your help. Rupert -- Posted via http://www.ruby-forum.com/.
2009/9/12 bingo bob <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Colin, You nailed it. Working great in my app - lovely. > Many thanks indeed for your help. > RupertGlad to be of help. Colin