List Info

Thread: Sorting results by foreign key




Sorting results by foreign key
country flaguser name
United States
2007-07-30 10:07:44
Hi Everyone,

I was wondering if anyone could lend a hand here.

I have two models, Group and Membership. As you can probably
guess:

class Group < ActiveRecord::Base
  has_many :memberships
  has_many :members,
           :through => :memberships
end

and ...

class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :group, :foreign_key => 'group_id'
end

I am trying to make a page Find() ALL the Groups, but then
order it by
the number of Memberships associated with that group. It's
for a "Sort
by popularity" function, which would output the list of
groups in
order of how popular they are (most memberships first).

I have no idea how one would do this using Rails. I'm sure
it is a
very simple solution but I can't get my head round it.
Because I am
tired maybe?

I hope I have explained this ok. Can anyone help?

Thanks and regards,

Edd
- http://www.eddm.co.uk


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


Re: Sorting results by foreign key
country flaguser name
United States
2007-07-31 08:18:07
one way around this might be to use counter_cache

so in your groups table you can have a column called
membership_count
and then when you define the relationship in your model you
put
something like belongs_to :group, :counter_cache => true

rails will automatically increment or decrement the column
when
objects are created or destroyed, and then you can just sort
by that
column in your database.

On Jul 30, 10:07 am, EddM <thew...googlemail.com> wrote:
> Hi Everyone,
>
> I was wondering if anyone could lend a hand here.
>
> I have two models, Group and Membership. As you can
probably guess:
>
> class Group < ActiveRecord::Base
>   has_many :memberships
>   has_many :members,
>            :through => :memberships
> end
>
> and ...
>
> class Membership < ActiveRecord::Base
>   belongs_to :user
>   belongs_to :group, :foreign_key => 'group_id'
> end
>
> I am trying to make a page Find() ALL the Groups, but
then order it by
> the number of Memberships associated with that group.
It's for a "Sort
> by popularity" function, which would output the
list of groups in
> order of how popular they are (most memberships
first).
>
> I have no idea how one would do this using Rails. I'm
sure it is a
> very simple solution but I can't get my head round it.
Because I am
> tired maybe?
>
> I hope I have explained this ok. Can anyone help?
>
> Thanks and regards,
>
> Edd
> -http://www.eddm.co.uk


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


Re: Sorting results by foreign key
user name
2007-07-31 08:39:26
Thanks I'll try that! I was trying to avoid storing a
counter as an
actual row in the db, but I wasn't aware Rails had a feature
to handle
it.

Thanks again,
Edd

On 7/31/07, Josh Kieschnick <jjkieschgmail.com> wrote:
>
> one way around this might be to use counter_cache
>
> so in your groups table you can have a column called
membership_count
> and then when you define the relationship in your model
you put
> something like belongs_to :group, :counter_cache =>
true
>
> rails will automatically increment or decrement the
column when
> objects are created or destroyed, and then you can just
sort by that
> column in your database.
>
> On Jul 30, 10:07 am, EddM <thew...googlemail.com> wrote:
> > Hi Everyone,
> >
> > I was wondering if anyone could lend a hand here.
> >
> > I have two models, Group and Membership. As you
can probably guess:
> >
> > class Group < ActiveRecord::Base
> >   has_many :memberships
> >   has_many :members,
> >            :through => :memberships
> > end
> >
> > and ...
> >
> > class Membership < ActiveRecord::Base
> >   belongs_to :user
> >   belongs_to :group, :foreign_key =>
'group_id'
> > end
> >
> > I am trying to make a page Find() ALL the Groups,
but then order it by
> > the number of Memberships associated with that
group. It's for a "Sort
> > by popularity" function, which would output
the list of groups in
> > order of how popular they are (most memberships
first).
> >
> > I have no idea how one would do this using Rails.
I'm sure it is a
> > very simple solution but I can't get my head round
it. Because I am
> > tired maybe?
> >
> > I hope I have explained this ok. Can anyone help?
> >
> > Thanks and regards,
> >
> > Edd
> > -http://www.eddm.co.uk
>
>
> >
>


-- 
Edd Morgan
thewzprgooglemail.com
+44 (0) 7979 474 043

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


Re: Sorting results by foreign key
country flaguser name
United States
2007-08-01 03:28:19
Hi Edd,

include Comparable

def <=>(otherinstance)
self.popularity <=> otherinstance.popularity
end

add above code into your model and define the comparator
operator

that should do the trick.

after getting your resultset you can just say results.sort
and your
dataset is sorted in the order you want

I would suggest you to do the relevant :include in your
find
statement, so you dont hit the db too many times.

Senthil

http://senthilnayagam.com
http://railsfactory.com




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )