First of all, thanks for your fast reply :)

Currently Im using a profile and friendship model with a modified 
version of acts_as_network(in the end of the reply). This is really bad 
code, and the query they provide to me when I want friends is not 
exactly what I want.
This queries 2 times the friendship table: first looks for all the 
profile_id column and then another query looks for friend_id column, and 
in this way is hard to sort, limit and offset data.
I would like to make a single sql request that looks for in the 
friend_id OR profile_id for that id. In this way it's easy to offset, 
limit and sort my data.

So.. this is the code I'm talking about :D



Profile model:
  has_many :friendships

  acts_as_network :friends,
              :through => :friendships,
              :foreign_key => 'profile_id',
              :association_foreign_key => 'friend_id',
              :conditions => {'friendships.accepted' => true}

  acts_as_network :friends_pending,
              :through => :friendships,
              :foreign_key => 'profile_id',
              :association_foreign_key => 'friend_id',
              :conditions => {'friendships.accepted' => false}


Frienship model:
  belongs_to :profile

  belongs_to :profile_target,
                :class_name => 'Profile',
                :foreign_key => 'friend_id'


Acts as network modified:
def acts_as_network(relationship, options = {})
          configuration = {
            :foreign_key => name.foreign_key,
            :association_foreign_key => "#{name.foreign_key}_target",
            :join_table => "#{name.tableize}_#{name.tableize}",
            :order => 'friendships.updated_at',
            :limit => 1,
            :offset => 2
          }
          configuration.update(options) if options.is_a?(Hash)

          if configuration[:through].nil?
            has_and_belongs_to_many "#{relationship}_out".to_sym, 
:class_name => name,
              :foreign_key => configuration[:foreign_key], 
:association_foreign_key => configuration[:association_foreign_key],
              :join_table => configuration[:join_table], :conditions => 
configuration[:conditions]

            has_and_belongs_to_many "#{relationship}_in".to_sym, 
:class_name => name,
              :foreign_key => configuration[:association_foreign_key], 
:association_foreign_key => configuration[:foreign_key],
              :join_table => configuration[:join_table], :conditions => 
configuration[:conditions]

          else
            through_class = configuration[:through].to_s.classify
            through_sym = configuration[:through]

            # a node has many outbound realationships
            has_many "#{through_sym}_out".to_sym, :class_name => 
through_class,
              :foreign_key => configuration[:foreign_key],
              :order => configuration[:order]

            has_many "#{relationship}_out".to_sym, :through => 
"#{through_sym}_out".to_sym,
              :source => "#{name.downcase}_target",  :foreign_key => 
configuration[:foreign_key],
              :conditions => configuration[:conditions],
              :order => configuration[:order],
              :select => 'profiles.*, friendships.updated_at AS 
friendship_updated_at, friendships.profile_id, friendships.friend_id, 
friendships.accepted AS friendship_accepted, friendships.rejected AS 
friendship_rejected'

            # a node has many inbound relationships
            has_many "#{through_sym}_in".to_sym, :class_name => 
through_class,
              :foreign_key => configuration[:association_foreign_key],
              :order => configuration[:order]

            has_many "#{relationship}_in".to_sym, :through => 
"#{through_sym}_in".to_sym,
              :source => name.downcase, :foreign_key => 
configuration[:association_foreign_key],
              :conditions => configuration[:conditions],
              :order => configuration[:order],
              :select => 'profiles.*, friendships.updated_at AS 
friendship_updated_at, friendships.profile_id, friendships.friend_id, 
friendships.accepted AS friendship_accepted, friendships.rejected AS 
friendship_rejected'

            # when using a join model, define a method providing a 
unioned view of all the join
            # records. i.e. if People acts_as_network :contacts :through 
=> :invites, this method
            # is defined as def invites
            class_eval <<-EOV
              def #{through_sym}
                UnionCollection.new(self.#{through_sym}_in, 
self.#{through_sym}_out)
              end
            EOV

          end

          # define the accessor method for the reciprocal network 
relationship view itself.
          # i.e. if People acts_as_network :contacts, this method is 
defind as def contacts
          class_eval <<-EOV
            def #{relationship}
              UnionCollection.new(self.#{relationship}_in, 
self.#{relationship}_out)
            end
          EOV
        end
-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "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.

Reply via email to