What does the followed_by construct mean?

I’m reading some code and it states…

#this can only occur on the person who was invited
def make_friends(invited, inviter)
transaction do
if user.followed_by? target

what does user.followed_by mean?

thanks!
-dan

On 20.01.2010 02:42, Dan Q. wrote:

I’m reading some code and it states…

#this can only occur on the person who was invited
def make_friends(invited, inviter)
transaction do
if user.followed_by? target

what does user.followed_by mean?

The code snippet you provided is far too short to tell you for sure.

A rough guess would be that #followed_by? checks if the target follows
the user described in the user object.

If you could provide more detail, it’d be appreciated. :slight_smile:

Phillip G. wrote:

On 20.01.2010 02:42, Dan Q. wrote:

I’m reading some code and it states…

#this can only occur on the person who was invited
def make_friends(invited, inviter)
transaction do
if user.followed_by? target

what does user.followed_by mean?

The code snippet you provided is far too short to tell you for sure.

A rough guess would be that #followed_by? checks if the target follows
the user described in the user object.

If you could provide more detail, it’d be appreciated. :slight_smile:

Sorry bout that… heres the full snippet
#this can only occur on the person who was invited
def make_friends(invited, inviter)
transaction do
if user.followed_by? target

      #update the existing pending relationship of 2 --> 1
      Friend.find(:first, :conditions => {:inviter_id => inviter.id, 

:invited_id => invited.id, :status =>
PENDING}).update_attribute(:status, ACCEPTED)

      #create the relationship of 1 --> 2
      Friend.create!(:inviter_id => inviter.id, :invited_id => 

invited.id, :status => ACCEPTED)
else
return add_follower(user, target)unless user.following? target
end
end
true
end

First, there is a board specific to rails, try
http://lists.rubyonrails.org/mailman/listinfo/rails

Here are my thoughts, though, and please understand that I cannot see
your
application, so I am simply hypothesizing.

Also, the method is presumably defined in app/models/user.rb and
examining
that method would shed more information.

Based on the context, my assumptions would be:

You have two users, that are using the site in some way.
One user may see how another user is using the site, and finds it
interesting (she has a cute screenname).
He decides to follow her, which invokes the ‘make_friends’ method.

At this point, he is the user and she is the target. She is not
following
him (she’s a cold mamba jamba), so it returns false. Which causes your
code
to add him as a follower to her, unless he is already following her
(stalker
alert). This creates a row in the friends table that has him as the
inviter,
and her as the invited. But she hasn’t accepted yet, so it sets the
status
column to PENDING.
For more info on how this is done, see
http://guides.rubyonrails.org/association_basics.html#self-joins or look
at
your Friend model.

She receives the friend request, and decides he is interesting (has a
sexy
avatar) So she accepts the friend request, or just goes to his
app/views/users/show.html.erb and follows him.

This invokes the ‘make_friends’ method, this time with her as the user,
and
him as the target. Since he sent her a friend request earlier,
user.followed_by?(target) returns true. It then finds that request, and
updates status to ACCEPTED and also creates another record, with her as
the
inviter, him as the invited, and status ACCEPTED, so there are two
records,
one from him to her, and one from her to him.

Your code apparently considers this to be friendship (heartwarming).

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs