SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Newbie Question about deleting records

    Hello,

    Following along with the Simply Rails 2 book, on page 138, talks about deleting records.

    I used the command (s.destroy) to delete one record, #2, but I can't it to delete the first record. Whey I type s.destroy(1), I get an Argument Error: wrong number of arguments (1 for 0)

    any ideas what I'm doing wrong? Just want to delete everything and re-enter the two records.

    Thanks

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,147
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Hi vhenry, welcome to the forums,

    AFAIK destroy doesn't take any arguments, but in this case is a method of the Story object.

    If you do
    Story.find(:all)
    do you see any rows?

    You can then use their id values like
    s = Story.find(id_value_here)
    then
    s.destroy

    But the "s" (or whatever you want to use as an object name value) represents one row in the table, not the table itself, so s.destroy(1) is meaningless.

  3. #3
    SitePoint Enthusiast TomK32's Avatar
    Join Date
    Mar 2008
    Location
    Berchtesgaden, Germany
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vhenry View Post
    I used the command (s.destroy) to delete one record, #2, but I can't it to delete the first record. Whey I type s.destroy(1), I get an Argument Error: wrong number of arguments (1 for 0)
    Please, always read the API closely. If you call destroy with a parameter then you must call it on the model not on the object.

    The following three work perfect:
    Code Ruby:
    Model.destroy(2)
    Model.find(2).destroy
    Model.destroy_all(2,3,5)
    just a geek trying to change the world
    I'm a ruby on rails developer in Vienna, Austria. My github.

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both, worked perfectly. Will get to know that API bit more intimately.

  5. #5
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    s.destroy worked because you were destroy that particular object. you would have needed to do s = Story.find(1) before s.destroy would destroy story with id of 1.


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
  •