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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 22, 2011, at 1:02 AM, Rob Gu wrote:> 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. >what is User.first.id ?> 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. > > -- > Posted via http://www.ruby-forum.com/. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 22 July 2011 09:05, Curtis Schofield <curtis-fRjNtq5pEao@public.gmane.org> 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... -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
"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! -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 22, 9:56 am, Rob Gu <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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> I''m wondering if as I was going through this tutorial, maybe I screwed > some things up? > > Thanks for the replies btw! > > -- > Posted viahttp://www.ruby-forum.com/.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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> > >On Jul 22, 9:56 am, Rob Gu <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 > > >> I''m wondering if as I was going through this tutorial, maybe I screwed >> some things up? >> >> Thanks for the replies btw! >> >> -- >> Posted viahttp://www.ruby-forum.com/. > >---- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, Jul 22, 2011 at 4:28 PM, Rob Gu <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder wrote in post #1012513:> On Fri, Jul 22, 2011 at 4:28 PM, Rob Gu <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > http://about.me/hassanschroeder > twitter: @hassanThat 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.