<% @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
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.
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.)
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.
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
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.
  <% @advert_types_sorted.each do |type, adverts| %>
  Â
    <%= type.inspect %>
  Â
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.