# What does the followed\_by construct mean?

**URL:** <https://www.ruby-forum.com/t/what-does-the-followed-by-construct-mean/181910>\
**Category:** Ruby\
**Created:** [January 20, 2010, 2:41am UTC](https://www.ruby-forum.com/t/what-does-the-followed-by-construct-mean/181910 "2010-01-20T02:41:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![palidanx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/97f17d/32.png) [@palidanx](https://www.ruby-forum.com/u/palidanx)\
**Post date:** [January 20, 2010, 2:41am UTC](https://www.ruby-forum.com/t/what-does-the-followed-by-construct-mean/181910/1 "2010-01-20T02:41:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![palidanx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/97f17d/32.png) [@palidanx](https://www.ruby-forum.com/u/palidanx)\
**Post date:** [January 20, 2010, 3:45am UTC](https://www.ruby-forum.com/t/what-does-the-followed-by-construct-mean/181910/2 "2010-01-20T03:45:20Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![palidanx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/97f17d/32.png) [@palidanx](https://www.ruby-forum.com/u/palidanx)\
**Post date:** [January 21, 2010, 6:38am UTC](https://www.ruby-forum.com/t/what-does-the-followed-by-construct-mean/181910/3 "2010-01-21T06:38:47Z")

</div>

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. 🙂

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

---

<div class="post-metadata">

**Author:** ![palidanx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/97f17d/32.png) [@palidanx](https://www.ruby-forum.com/u/palidanx)\
**Post date:** [January 21, 2010, 7:33am UTC](https://www.ruby-forum.com/t/what-does-the-followed-by-construct-mean/181910/4 "2010-01-21T07:33:23Z")

</div>

First, there is a board specific to rails, try  
[http://lists.rubyonrails.org/mailman/listinfo/rails](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  
[Active Record Associations — Ruby on Rails Guides](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).
