I''m trying to create a simple blog app without scaffolding, and I''m having problems with associations. I''m relatively new to this, and I would love some help. I''m sorry for bugging you with such a newbie question, but I couldn''t find a tutorial about this. I made my models like this: model g User username password model g Admin model g Poster model g Post title content then, I set the associations like this: User has_many :admins has_many :users Poster belongs_to :User Post has_many :posts belongs_to :Poster Admin has_many: users then, when I go to the rails console, I cant make a post that belongs to poster, or a post that belongs to poster... User.create(:name => "joe") usr = User.last usr.Poster.create() ---- returns an error What am I doing wrong here? -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/qb_bLsAkmycJ. For more options, visit https://groups.google.com/groups/opt_out.
On 22 October 2012 06:11, lyosha <lyosha85-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m trying to create a simple blog app without scaffolding, and I''m having > problems with associations. > > I''m relatively new to this, and I would love some help. I''m sorry for > bugging you with such a newbie question, but I couldn''t find a tutorial > about this.Have a look at the Rails Guide on ActiveRecord Associations. It should help.> > I made my models like this: > model g User username password > model g Admin > model g Poster > model g Post title content > > then, I set the associations like this: > User > has_many :admins > has_many :users > > Poster > belongs_to :UserIf poster belongs_to user then you should also have user has_many or has_one poster. However, going solely on the names of the classes are you sure you want a posters table? Is a poster just a user?> > Post > has_many :postsWhat?> belongs_to :PosterAgain you have not got the matching has_many> > Admin > has_many: usersAgain this does not match the spec in users.> > then, when I go to the rails console, I cant make a post that belongs to > poster, or a post that belongs to poster... > > User.create(:name => "joe") > usr = User.last > usr.Poster.create() ---- returns an error > > What am I doing wrong here?I think it would be worth your while working right through some tutorails. railstutorial.org is good and is free to use online. Make sure that any tutorial you use is for rails 3 and that you have the right version of rails installed. Colin -- 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 https://groups.google.com/groups/opt_out.
On Mon, Oct 22, 2012 at 3:48 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> are you sure you want a posters table? Is a poster just a user?Taking a positive spin: the original poster (no pun intended) might be trying to separate concerns, putting the authentication, authorization, and accounting stuff in User, and the stuff having to do with posting, in Poster, thus preventing a common antipattern of User becoming a God Object. I started doing something similar with one of my side projects, until I found out that Heroku now limits the databases of free apps to a number of *rows*, rather than *bytes* like they used to. :-) -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/. -- 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 https://groups.google.com/groups/opt_out.