# Beginning ruby session problem / how do I rollback with git?

**URL:** <https://www.ruby-forum.com/t/beginning-ruby-session-problem-how-do-i-rollback-with-git/166294>\
**Category:** Rails\
**Created:** [April 23, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/beginning-ruby-session-problem-how-do-i-rollback-with-git/166294 "2009-04-23T16:44:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [April 23, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/beginning-ruby-session-problem-how-do-i-rollback-with-git/166294/1 "2009-04-23T16:44:54Z")

</div>

Hi,

Rails 2.3.2

I’m following along with the examples in AWDWR (3rd), and around about  
p. 103, where you are working with a session table you created, I  
decided to free lance a bit. At some point, I tried to “clear” the  
session by doing the following:

class StoreController \< ApplicationController  
…

private  
def find\_cart  
#session[:cart] ||= Cart.new  
session[:cart] = Cart.new #\<------\*\*\*\*\*\*\*\*  
end

I figured that would “erase” the old cart, and let me continue with a  
new, empty cart. However, everything is completely messed up now, and  
when I try to access a “store page” (which lists the products for sale)  
using the following url:

[http://localhost:3000/store](http://localhost:3000/store)

I get an error page that says:

We’re sorry but something went wrong.  
We’ve been notified about this issue and  
we will take a look at it shortly.

As a fix, I tried rolling back the migration that created the session  
table:

rake db:rollback

and then doing:

db:migrate

but I get that same error page. So I’m stuck. So much for free  
lancing. Is there some obvious way I can get rid of that error page and  
get sessions working again.

Alternatively, I have git installed and I committed my changes at the  
end of the last chapter(p. 96). But I’ve read several git tutorials,  
and I can’t figure out how to rollback to a previous version of my code.

Thanks.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [April 23, 2009, 4:53pm UTC](https://www.ruby-forum.com/t/beginning-ruby-session-problem-how-do-i-rollback-with-git/166294/2 "2009-04-23T16:53:40Z")

</div>

7stud – wrote:

> Hi,
> 
> Rails 2.3.2
> 
> However, everything is completely messed up now, and  
> when I try to access a “store page” (which lists the products for sale)  
> using the following url:
> 
> [http://localhost:3000/store](http://localhost:3000/store)
> 
> I get an error page that says:
> 
> We’re sorry but something went wrong.  
> We’ve been notified about this issue and  
> we will take a look at it shortly.

Sorry, I can access the store page. It’s when I click a button that  
calls an add\_to\_cart action that I get that error message. This is the  
url after I click on the add\_to\_cart button:

[http://localhost:3000/store/add\_to\_cart/5](http://localhost:3000/store/add_to_cart/5)

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [April 23, 2009, 4:56pm UTC](https://www.ruby-forum.com/t/beginning-ruby-session-problem-how-do-i-rollback-with-git/166294/3 "2009-04-23T16:56:00Z")

</div>

7stud – wrote:

> Sorry, I can access the store page. It’s when I click a button that  
> calls an add\_to\_cart action that I get that error message. This is the  
> url after I click on the add\_to\_cart button:
> 
> [http://localhost:3000/store/add\_to\_cart/5](http://localhost:3000/store/add_to_cart/5)

Here is the add\_to\_cart action:

class StoreController \< ApplicationController  
def index  
@products = Product.find\_products\_for\_sale

```
now = Time.now
@date = now.strftime("%m/%d/%Y")
@time = now.strftime("%I:%M%p")

```

end

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)

end

private  
def find\_cart  
session[:cart] ||= Cart.new  
end  
end

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [April 23, 2009, 8:02pm UTC](https://www.ruby-forum.com/t/beginning-ruby-session-problem-how-do-i-rollback-with-git/166294/4 "2009-04-23T20:02:53Z")

</div>

I figured out how to rollback with git from this website:

[http://www-cs-students.stanford.edu/~blynn/gitmagic/ch02.html#\_advanced\_undo\_redo](http://www-cs-students.stanford.edu/~blynn/gitmagic/ch02.html#_advanced_undo_redo)

$ git log  
commit 1964b4f78d82da730ccf5fc6d1ba7d1e1b5ae266  
Author: me  
Date: Wed Apr 22 18:57:48 2009 -0600

```
End of chapter 7, p. 96

```

commit a262fdcd503766cae2c5643923180480151ff669  
Author: me  
Date: Tue Apr 21 03:28:49 2009 -0600

```
Initial import

```

/depot$ git reset --hard 1964b4f78d82da730ccf5fc6d1ba7d1e1b5ae266  
HEAD is now at 1964b4f End of chapter 7, p. 96  
$

However, apparently doing a rollback doesn’t delete the new files you  
created since the last commit. So, I also deleted the migration file  
for creating the sessions table.

Then I started over with the steps at the beginning of the chapter.  
After adding all the necessary code, I clicked on the add to cart  
button, and I get the same error message:

[http://localhost:3000/store/add\_to\_cart/5](http://localhost:3000/store/add_to_cart/5)

We’re sorry, but something went wrong.  
We’ve been notified about this issue and we’ll take a look at it  
shortly.

So it seems like there is something wrong with my database.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [April 23, 2009, 6:38pm UTC](https://www.ruby-forum.com/t/beginning-ruby-session-problem-how-do-i-rollback-with-git/166294/5 "2009-04-23T18:38:49Z")

</div>

7stud – wrote:

> private  
> def find\_cart  
> session[:cart] ||= Cart.new  
> end

If I change that method to:

def find\_cart  
Cart.new  
end

then clicking on an “add to cart” button no longer shows me the error  
message. I get a view that lists what was added to the cart. However,  
without sessions the view just shows the current item that is being  
added.

As soon as I add back the line that mentions sessions:

> private  
> def find\_cart  
> session[:cart] ||= Cart.new  
> end

I get the error message.

I tried rolling back the migration again:

rake db:rollback

then deleting the migration file:

/depot/db/migrate$ 20090423153751\_create\_sessions.rb

then recreating the migration file:

/depot$ rake db:sessions:create

and recreating the table:

/depot$ rake db:migrate

then restarting the server. But that did not solve my problem.

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [April 23, 2009, 8:23pm UTC](https://www.ruby-forum.com/t/beginning-ruby-session-problem-how-do-i-rollback-with-git/166294/6 "2009-04-23T20:23:06Z")

</div>

7stud – wrote:

> So it seems like there is something wrong with my database.

I tried rolling back my migrations to the beginning:

# /depot$ rake db:migrate VERSION=0 (in /Users/me/2testing/dir1/rails/depot) == CreateSessions: reverting

# – drop\_table(:sessions) -\> 0.0014s == CreateSessions: reverted (0.0016s)

# == AddTestData: reverting

# == AddTestData: reverted (0.0034s)

# == AddPriceToProduct: reverting

# – remove\_column(:products, :price) -\> 0.0263s == AddPriceToProduct: reverted (0.0265s)

# == CreateProducts: reverting

# – drop\_table(:products) -\> 0.0010s == CreateProducts: reverted (0.0012s)

which left me with this:

/depot$ sqlite3 db/development.sqlite3

SQLite version 3.6.13  
Enter “.help” for instructions  
Enter SQL statements terminated with a “;”

sqlite\> .mode line  
sqlite\> select \* from sqlite\_master;

```
type = table
name = schema_migrations

```

tbl\_name = schema\_migrations  
rootpage = 2  
sql = CREATE TABLE “schema\_migrations” (“version” varchar(255) NOT  
NULL)

```
type = index
name = unique_schema_migrations

```

tbl\_name = schema\_migrations  
rootpage = 3  
sql = CREATE UNIQUE INDEX “unique\_schema\_migrations” ON  
“schema\_migrations” (“version”)

```
type = table
name = sqlite_sequence

```

tbl\_name = sqlite\_sequence  
rootpage = 5  
sql = CREATE TABLE sqlite\_sequence(name,seq)

Then I recreated all the tables:

# /depot$ rake db:migrate (in /Users/me/2testing/dir1/rails/depot) == CreateProducts: migrating

# – create\_table(:products) -\> 0.0019s == CreateProducts: migrated (0.0021s)

# == AddPriceToProduct: migrating

# – add\_column(:products, :price, :decimal, {:precision=\>8, :scale=\>2, :default=\>0}) -\> 0.0118s == AddPriceToProduct: migrated (0.0122s)

# == AddTestData: migrating

# == AddTestData: migrated (0.0127s)

# == CreateSessions: migrating

# – create\_table(:sessions) -\> 0.0015s – add\_index(:sessions, :session\_id) -\> 0.0004s – add\_index(:sessions, :updated\_at) -\> 0.0004s == CreateSessions: migrated (0.0027s)

Then If I use this version of the find\_cart method in the store  
controller:

class StoreController \< ApplicationController  
def index  
@products = Product.find\_products\_for\_sale  
now = Time.now  
@date = now.strftime("%m/%d/%Y")  
@time = now.strftime("%I:%M%p")  
end

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)  
end

#\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
private  
def find\_cart  
Cart.new  
end  
#\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
end

when I click on my “add to cart” button, a view displays the current  
item. However, if I try to use sessions:

#\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
private  
def find\_cart  
session[:cart] ||= Cart.new  
end  
#\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

then I get the error message. The only thing I haven’t tried is to  
delete my whole project and start over.
