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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
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.

Search Discussions

  • Colin Law at Oct 22, 2012 at 7:49 am

    On 22 October 2012 06:11, lyosha 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 :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

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dave Aronson at Oct 22, 2012 at 2:52 pm

    On Mon, Oct 22, 2012 at 3:48 AM, Colin Law 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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedOct 22, '12 at 5:16a
activeOct 22, '12 at 2:52p
posts3
users3
websiterubyonrails.org
irc#RubyOnRails

3 users in discussion

Dave Aronson: 1 post Colin Law: 1 post Lyosha: 1 post

People

Translate

site design / logo © 2021 Grokbase