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?
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 :User
If 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 :posts
What?
belongs_to :Poster
Again you have not got the matching has_many
Admin
has_many: users
Again 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
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