Solved

Rails console, saving objects

Posted on 2008-10-05
1
1,000 Views
Last Modified: 2013-11-13
I was reading a book and following the instructions in it.

>> class Story < ActiveRecord::Base; end
=> nil
>> story = Story.new
=> #<Story id: nil, name: nil, url: nil, created_at: nil, updated_at: nil>
>> story.class
=> Story(id: integer, name: string, link: string, created_at: datetime, updated_at: datetime)

I do not remember me asking for id, name, link or url, created_at and updated_at. Is there a way to check if I asked?

I do know if I typed craeted_at instead of created_at or updetad_at instead of updated_at. Is there a way to check whether I typed it right or wrong?

From the Rails console, I had to create a story class, one instance of it and save the object, but I created two, so I got a 2 output for a story.id input. How do I remove the second object from the database?
0
Comment
Question by:locke1994
1 Comment
 
LVL 14

Accepted Solution

by:
wesgarrison earned 375 total points
ID: 22647317
Check out your migration in db/migrate.
There should be one called "create_stories" or something similar.

You can change that file and then rerun the migrations with 'rake db:reset'.

If you want to delete an object, just call the destroy on it or via the class:

# delete an instance
story = Story.find(2)
story.destroy
 
# delete via class method
Story.destroy(2)
 
# Both do the exact same thing

Open in new window

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question