SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Inteligent rails :)

    While i was developing the sitepoint's rails book example i added a code line by me to save the author id in the stories table column "author_id" automatically when a new story is added through the form ('cause the example doesn't add this functionality yet).
    I added:

    @story.author_id = @current_user


    ...where @story is the current story object and @current_user holds the object of the user currently logged in. The magic is that i didn't need to specify which author database column value to assign to the story author_id column with: "@current_user.id"


    Another strange book code thing is this:

    Code:
    @story = Story.find(params[:id])
    @story.votes.create(:user => @current_user)
    in the second line i have to create a row in the votes table with predefined values for two column plus the current user_id in the user_id column of the votes table. The code specifies ":user => @current_user" and the table column name isn't specified clearly (only :user and not :user_id) and again the @current_user instead of @current_user.id

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can even do:

    Code:
    @story.author = @current_user
    No need for _id at all

  3. #3
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fenrir2 View Post
    You can even do:

    Code:
    @story.author = @current_user
    No need for _id at all
    so in all the code above the magic happens thanks to the _id or id not nedded? It is fantastic!

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well it's not really magic, it's just an abstraction. @story.author= is the same as @story.author_id=. It's easier to think in terms of authors than in terms of author_id's.

  5. #5
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok but if i write:

    @story.author = @current_user

    ....how can rails knows which @current_user attribute to use?

  6. #6
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Skyblaze View Post
    @current_user holds the object of the user currently logged in
    Quote Originally Posted by Skyblaze View Post
    ok but if i write:
    @story.author = @current_user
    ....how can rails knows which @current_user attribute to use?
    @current_user probably holds a bunch of values about the user (I don't have the book) -- one of which is the current user's id. One of Rails' conventions is that model objects have a primary key called "id" (although this can be explicitly overridden in a model class definition). If you follow this naming convention you can say:
    Code:
    @story.author = @current_user
    simply for following the "id" conventions that Rails expects. In this case, Rails "automagically" assigns the @story instance variable's author field to the id of @current_user. You get rewarded with writing less code simply by following the convention of the framework.

    Does that make any sense?

  7. #7
    SitePoint Guru Skyblaze's Avatar
    Join Date
    Jul 2005
    Location
    Italy
    Posts
    734
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyway now that i'm finishing the book i think i understand why these things happen. In reality i think rails with this:

    @story.user = @current_user

    ...is doing internally:

    @story.user_id = @current_user.id


    ...but we use "@story.user" to reference the User model object associated with the current story not the "user_id" column or attribute of the Story model directly.
    So using:

    @story.user = @current_user

    we are saying in general that we want that the User associated with the current Story is the current user (logged in) in the instance variable @current_user then rails interanlly does:

    @story.user_id = @current_user.id



    is that true?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •