Association beween model

hi
maybe its a simple question, but i can’t understand how to resolve it.
i have a model user and contact.
so a user can have many contact and contact belong to one user.
so for now i want count the number of contact of the user.
what i did is like this:
count = ActiveRecord::Base.connection.select_one(“select
count(person_id) amis from contacts where user_id =” + user.id.to_s)

but i dont like this solutions cause the number of freind i will use
it in a sortable table, and if this variable dont belong to the user
params, i cant use it to sort the table :confused:
thanks.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

In the controller, get the user in question, for example

@user = User.find(params[:id])

and then do

count = @user.contacts.count

Wael

On May 25, 2011, at 2:23 PM, Adel M. wrote:

it in a sortable table, and if this variable dont belong to the user
params, i cant use it to sort the table :confused:
thanks.


You received this message because you are subscribed to the Google G. “Ruby
on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org

iEYEARECAAYFAk3c9zwACgkQVWU5RcjdGKLcpwCdEm70lLXwvWvPOgm7CWgqISlY
gugAoPplH3jIwb/xX9D7mRa/x/dJzuYT
=QFh9
-----END PGP SIGNATURE-----

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hey Adel,

I’m sorry I’ve never used this gem, but maybe try creating a count
method on the user model.

Wael

On May 25, 2011, at 6:04 PM, Adel M. wrote:

Wael

it in a sortable table, and if this variable dont belong to the user
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org

iEYEARECAAYFAk3eSTIACgkQVWU5RcjdGKL+wwCfVTqAwT/eCd8G/Gs0nUTvkXS/
fDQAnRP4EjSDk8eIHEqA/uAyEiuJVRbF
=pVEw
-----END PGP SIGNATURE-----

thanks Wael :slight_smile:
it make the half of work.
the probleme now is when i use the gems handles_sortable_columns i
can’t sort the table with the count column cause the count field dont
figure in the user table :S
i didn’t find a solution !!