pagination joining more than two tables

Are you not creating a many to many with this table setup. In which case, if you use the rails methods it will reduce the amount of thinking you need to do to access the various relationships:

Unless I have misunderstood you:

class Group < ActiveRecord::Base   has_many :user_groups   has_many :contacts, :through=>:user_groups end

class Contact < ActiveRecord::Base   has_many :groups, :through=>:user_groups   has_many :user_groups end

class UserGroup < ActiveRecord::Base   belongs_to :group   belongs_to :contact end

So that you can do @group=Group.find(params[:group_id]) @contacts=@group.contacts.paginate(:page=>params[:page])

(if you don't need @group, you could combine this into one line) Although, that is 2 fetches from the database, I reckon that is easier to see


Hi   Thanks for all your reply..I did it as tonypm Sijo

Another option I've followed for a number of "index" views is to create a database view that can hide the complexity of multiple joins behind a single model.

In one view, users requested data from the current model, that model's parent, and the parent's parent, and a summary off a child table.

The database view hides all that nastiness, even from rails, and keeps the controller code clean as well.

Might be overkill in your case, but for bigger conglomerations of data, it hurt to see all the SQL generated by rails.