Active Record Issue (I think)

I’m following a rails tutorial (Hartl) and it’s been fine so far, but
something doesn’t seem to be working right.

I have two basic models so far, “User” and “Micropost”.

class User < ActiveRecord::Base
has_many :microposts
end

class Micropost < ActiveRecord::Base
belongs_to :user
end

If I do something like:
“first_user = User.first”
Then:
“first_user.microposts” - should return a hash of the first user’s
microposts, but it just returns empty.

I’m sure there is data.

If I do “Micropost.first” it returns what I should see with
“first_user.microposts”. If I do “Micropost.first.user_id” it does
return “1”, so I know the association is there.

I did kill the rails server by pressing control+c, and then restarted
but that didn’t make any difference.

Any ideas? I’m on Ubuntu 10.04/Ruby version 1.8.7/RubyGems 1.8.5/Rack
version 1.2/Rails version 3.0.9 if that helps.

On Jul 22, 2011, at 1:02 AM, Rob Gu wrote:

belongs_to :user
end

If I do something like:
“first_user = User.first”
Then:
“first_user.microposts” - should return a hash of the first user’s
microposts, but it just returns empty.

what is User.first.id ?

On 22 July 2011 09:05, Curtis S. [email protected] wrote:

On Jul 22, 2011, at 1:02 AM, Rob Gu wrote:

If I do something like:
“first_user = User.first”
Then:
“first_user.microposts” - should return a hash of the first user’s
microposts, but it just returns empty.

what is User.first.id ?

or Micropost.first.user ?

…I think Curtis and I have the same thought going through our heads,
that
maybe the first user’s id isn’t 1…

“User.first.id” returns “3”, I guess I did some edit/deletes.

“Micropost.first.user” returns “nil”.
“Micropost.first.user_id” returns “1” oddly enough.

Interesting, but what I don’t get, is “User.first” selects the first
user (regardless of their id). So “User.first.microposts” should give
me the first user’s microposts I would think, no?

I’m wondering if as I was going through this tutorial, maybe I screwed
some things up?

Thanks for the replies btw!

I got confused since when I went to (localhost)/microposts, it showed me
the list of microposts with a “User” column with values of 1’s and 2’s
(I have two users), but in actuality their id’s are 3 an 5 respectively,
so I’m not sure why there is a discrepancy.

On Jul 22, 9:56am, Rob Gu [email protected] wrote:

“User.first.id” returns “3”, I guess I did some edit/deletes.

“Micropost.first.user” returns “nil”.
“Micropost.first.user_id” returns “1” oddly enough.

Interesting, but what I don’t get, is “User.first” selects the first
user (regardless of their id). So “User.first.microposts” should give
me the first user’s microposts I would think, no?

If User.first.id is 3, then User.first.microposts will return the
microposts with user_id 3
It sounds like you used to have a User with id 1, which you then
deleted, leaving an orphaned micropost with no user behind.

Fred

Ok thanks guys for the great advice, that is almost certainly the issue
(creating users and microposts, then deleting the original user etc…)

I was actually planning to start messing with MySQL so I’ll look into
SequelPro.

Again thank you everyone!

flyVing

I occasionally forget that I deleted some record and am puzzled why
something isn’t working. I am using MySQL and I find that it’s helpful
to have an easy way to look at the specifics of what’s in the database
so I keep SequelPro (http://www.sequelpro.com/) running. Then I can
see what the PK and FK values are at a glance (“yes, I did delete User
with ID=1”, for example). It also makes it easy to create test records
quickly while I’m working out various algorithms.

**Leigh

Hassan S. wrote in post #1012513:

On Fri, Jul 22, 2011 at 4:28 PM, Rob Gu [email protected] wrote:

Ok thanks guys for the great advice, that is almost certainly the issue
(creating users and microposts, then deleting the original user etc…)

Regardless of DB, remember you can handle situations like this using
e.g. :dependent => :destroy on an association, or even an after_destroy
callback (if you want to do something with the “orphaned” object").


Hassan S. ------------------------ [email protected]
Hassan Schroeder | about.me
twitter: @hassan

That is insightful, thanks. I was actually wondering about some sort of
cascade deletion method, seems like that would be it. The after_destroy
method seems like it could be useful too.

On Fri, Jul 22, 2011 at 4:28 PM, Rob Gu [email protected] wrote:

Ok thanks guys for the great advice, that is almost certainly the issue
(creating users and microposts, then deleting the original user etc…)

Regardless of DB, remember you can handle situations like this using
e.g. :dependent => :destroy on an association, or even an after_destroy
callback (if you want to do something with the “orphaned” object").


Hassan S. ------------------------ [email protected]

twitter: @hassan