0

I am building a database architecture. I also have read this guide So I will have following models:

Author

name, has_many:questions, has_many:comments, has_many:edits

Question

title, body , author_id, has_many:comments, belongs_to:author

Comment

body, question_id, author_id, belongs_to:question, has_many:edits, belongs_to:author

Edit

body, comment_id, author_id, belongs_to:comment, belongs_to:author

As you can see, question can have many comments and each comment can have many edits. And author can have many questions, edits and comments

Main question is: did I use has_many and belongs_to assosiations correctly?!

Additional info: I don't know if it is relevant, but I want to use following scaffold migrations:

rails generate scaffold Question title:string body:text author_id:integer has_many:comments belongs_to:author

rails generate scaffold Comment body:text question_id:integer author_id:integer belongs_to:question has_many:edits belongs_to:author

rails generate scaffold Edit body:text comment_id:integer author_id:integer belongs_to:comment belongs_to:author

rails generate scaffold Author name:string has_many:questions has_many:comments has_many:edits

UPD: My scaffolds are incorrect. I should use something like this:

rails generate scaffold Question title:string body:text user:references rails generate scaffold Comment body:text user:references question:references
rails generate scaffold Edit body:text user:references question:references

As suggested here

Community
  • 1
  • 1
user2950593
  • 9,233
  • 15
  • 67
  • 131

1 Answers1

0

Both your associations and scaffolds are correct. I would just advice to use dependent: :destroy after all your belongs_to associations so when for example an author is deleted all his questions are deleted from you database aswell.

For example:

Class Question < ActiveRecord::Base
 belongs_to :author, dependent: :destroy
end

That way you can avoid a big mess in your database.

luissimo
  • 916
  • 2
  • 10
  • 31