Solved

Rails console, saving objects

Posted on 2008-10-05
1
993 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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
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 discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now