-1

I have schema like this:

create_table "grades", force: :cascade do |t|
    t.integer "cls"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "post_grades", force: :cascade do |t|
    t.integer "post_id"
    t.integer "grade_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.integer "user_id", null: false
    t.string "title"
    t.text "content"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

create_table "user_grades", force: :cascade do |t|
    t.integer "user_id"
    t.integer "grade_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

In this schema grades, posts and users all are related to each other.

So my first question: Is it correct way of doing this?

Suppose one of user added grade 4 (cls = 4) in his column and another user added same grade. Now I have same value of cls in grade table for two different grade ids.So is there any data redundancy in this schema?

Vishal Goel
  • 170
  • 1
  • 1
  • 10

1 Answers1

0

As I got your task, you can go with such solution:

  1. grades -> no references (as it is right now)
  2. user_grades (conventional way to name this table would be grades_users... alphabetical order and both in plural) -> user_id, grade_id (as it is right now)
  3. You don't need post_grades. It's redundant
  4. posts -> grade_id, user_id (if you need author)

Your schema.rb will look smth like:

create_table "users", force: :cascade do |t|
  ...
end

create_table "grades_users", force: :cascade do |t|
  t.integer "user_id"
  t.integer "grade_id"
  ...
end

create_table "grades", force: :cascade do |t|
  ...
end

create_table "posts", force: :cascade do |t|
  t.integer "user_id", null: false
  t.integer "grade_id", null: false
  ...
end
AntonTkachov
  • 1,784
  • 1
  • 10
  • 29
  • Suppose one of user added in grade (cls: 4) in his column and another user added same grade. Now I have same value of cls in grade table for two different grade ids.So is this also data redundancy ? – Vishal Goel May 02 '18 at 22:00
  • No, it's not data redundancy, because following such approach you can end up that all duplicate data is redundancy. Do you need `Grade` to be created only with unique `cls` field? – AntonTkachov May 03 '18 at 17:05
  • No..for a particular User ..Grade is created only with unique cls field but of course different user can have same cls field as other User has.. – Vishal Goel May 03 '18 at 17:45
  • Then everything is all right. And, please, let's get back to your initial question. Right now it's already a new question. Have my answer helped you? If yes, then please mark it with a tick and up vote for future searches. Lost of thanks in advance. – AntonTkachov May 03 '18 at 17:50