ga-dc / wdi5-project3

Project 3
0 stars 5 forks source link

AR nested tables ordering by column #80

Closed lmdragun closed 9 years ago

lmdragun commented 9 years ago

I have a users table, an ownerships join table, and a comics table, so that users have comics through ownerships. On the user's show page, I can output the comics by getting the join table information ('c.comics.series' for example), but I want to order them alphabetically by series (which is in the comics table).

This is what I have right now, but it's not working (the first two lines work):

    @user = User.find(params[:id])
    @owned_comics = @user.ownerships.all
    @ordered_comics = @owned_comics.comics.order(series: :desc)

I also tries ("series DESC") and a few other ways.

RobertAKARobin commented 9 years ago

Do you get an error?

andrewsunglaekim commented 9 years ago

you could always make a custom method in your model definitions as well. IE in your comic.rb :

def order_series_by_alpha
  # some code that gets all of the series and orders them
end

Then you could just do @ordered_comis = @ownd_comics.comics.order_series_by_alpha ... or something like that.

andrewsunglaekim commented 9 years ago

Cool, i think we solved this issue, open another issue if you run into more problems with this issue or reopen