1

Forgive me if this is a newb question but I was wondering how they got the current user's id in the User model here:
Listing 10.39 I've tried reading it again and again and i still can't figure it out :(

class User < ActiveRecord::Base
  .
  .
  .
  def feed
    # This is preliminary. See "Following users" for the full implementation.
    Micropost.where("user_id = ?", id)
  end
  .
  .
  .
end
ekremkaraca
  • 1,453
  • 2
  • 18
  • 37
Skyalchemist
  • 461
  • 1
  • 7
  • 18

1 Answers1

1

feed is an instance level method so self will have user object. So id is equivalent to self.id.

For example: Assume you have user_method in User model.

def user_method
  puts self  #prints user object
  puts self.id #prints user id
  puts id #prints user id
end

user = User.create(user_attributes)
user.user_method

Similarly, feed is called on some user object.

Rahul Tapali
  • 9,887
  • 7
  • 31
  • 44
  • I don't still understand :( Feed is an instance method (I get) of class User which extends ar base. I believe "self" is sort of like "this" in php right? Why does self have the current user's object? – Skyalchemist Mar 08 '13 at 19:09
  • Oh thanks, didnt see your edit. Yh i understand the self part now. If i'm current, the class User stores the object when you perform User.create or authenticate right? One more ques, could i get the id in another model also? – Skyalchemist Mar 08 '13 at 19:24
  • @Skyalchemist `id` of what? – Rahul Tapali Mar 08 '13 at 19:25
  • The id of the current user. – Skyalchemist Mar 08 '13 at 21:07
  • Micropost.where("user_id = ?", id), the id there, that's what i'm asking about. Where it comes from and how the model knows its the current user's id. From what you said, i'm not sure but i'm assuming the User model keeps the current user's object somehow when a user signs in or signs up. And calling self.id in the User model class will give the current user's id. That's my take on it currently, i'm not sure if i'm right or wrong. – Skyalchemist Mar 08 '13 at 21:15
  • 1
    let me try it... when you call feed, you are calling it on an object which is an instance of User class **this object** responds to **id** as well. Inside feed when you call **id** you actually calling **self.id** and self is the object you call feed on. id is a table column that rails automatically adds when you are creating db table. its unique. – Kocur4d Mar 09 '13 at 01:54
  • 1
    @Skyalchemist Method `feed` is in `user` model. So, `self` will have user object not `micropost` object. And `current_user` is set by your authentication method at application level or Controller level when user logins successfully. Somewhere in controller they are calling `current_user.feed` so current_user has some user object. You can call `feed` on any user object I mean it doesn't need to be always `current_user`. – Rahul Tapali Mar 09 '13 at 02:37
  • @Skyalchemist In simple `self` is like `this` and in instance_level methods `this` is `implicit` is used when you are calling instance methods. `id` is also a instance method. Every attribute in rails will have three methods by default `setter(i.e. id=)`, `getter (i.e. id)`, `boolean method (i.e. id?) returns true if id is not nil else returns false`. So when you are using `id` it calls instance method `getter id`. – Rahul Tapali Mar 09 '13 at 02:38