-1

SOLVED Please read more at the end of the question and in my answer below.

My question is that what will be relationship between user,assessment,question,answer with user_answer

Relationship which I had defined

  • One assessment has_many questions
  • One question has_many answers

My Scheme file

create_table "assessments", force: :cascade do |t|
    t.string   "name"
    t.text     "description"
end

create_table "answers", force: :cascade do |t|
    t.integer  "question_id"
    t.string   "answer_label"
    t.integer  "answer_value"
end

create_table "questions", force: :cascade do |t|
    t.integer  "assessment_id"
    t.text     "effective_text"
    t.text     "ineffective_text"
end

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

create_table "user_answers", force: :cascade do |t|
    t.integer  "assessment_id"
    t.integer  "question_id"
    t.integer  "answer_id"
    t.integer  "user_id"
end

Help me, If you can

Thanks

Gupta
  • 8,882
  • 4
  • 49
  • 59
Jigar Bhatt
  • 4,217
  • 2
  • 34
  • 42
  • Your question really isn't clear to me, but just as a reminder, the join table should be `answers_users` rather than `users_answers`. Relations are *always* in alphabetical order when you do things the Rails way. – David Hoelzer Dec 25 '15 at 13:29
  • @David Hoelzer : Let me know which part of this question you don't understand? – Jigar Bhatt Dec 25 '15 at 13:30
  • @DavidHoelzer : Here just i want to know relationship between user_answer with other tables? – Jigar Bhatt Dec 25 '15 at 13:31
  • Go through [Active Record Associations](http://guides.rubyonrails.org/association_basics.html) chapter from Rails Guides – Wand Maker Dec 25 '15 at 13:44
  • Ok ..So need the ruby code on following schema ?? M I Correct – Gupta Dec 25 '15 at 13:55
  • @Vinay : Nope, I want to know what is the relation between user and user_answer AND what is the relation between assessment and user_answer? – Jigar Bhatt Dec 25 '15 at 14:11

1 Answers1

1

Let me answer to your Question One by one

I want to know what is the relation between user and user_answer

class User < ActiveRecord::Base
  has_many :answers, through: user_answers
  has_many :assessment, through: user_answers
end

class UserAnswer < ActiveRecord::Base
  belongs_to :answer
  belongs_to :user
  belongs_to :assessment
end

what is the relation between assessment and user_answer?

class Assessment < ActiveRecord::Base
  has_many :users, through: user_answers
end

I would strongly suggested to you read out Stackoverflow- Can a foreign key be NULL and/or duplicate?

Hope this help you !!!

Community
  • 1
  • 1
Gupta
  • 8,882
  • 4
  • 49
  • 59
  • @Vijay : Thanks for reply. Please explain me what relationship you have given? Many-many relationship between User and Assessement? Many-many relationship between User and Answer? Middle table is UserAnswer Am i right? – Jigar Bhatt Dec 26 '15 at 06:17
  • @JigarBhatt First and Foremost one I m not _Vijay_ :P ..Okey As you might asked I answered your exact Question, As you had created _join table_ as _user_answer_ . I really dont think this is a best way to save the record.Because For each records for _user_answer_ would have at-least one foreign_key would be nil. – Gupta Dec 26 '15 at 06:20
  • i am sorry. class Answer < ActiveRecord::Base has_many :users, through: user_answers end is this code fine for answer model? – Jigar Bhatt Dec 26 '15 at 06:51
  • Thanks you so much for your help. You are Genious. – Jigar Bhatt Dec 26 '15 at 07:02
  • 1
    Welcome But i must say I'm Also in process of Learning .Glad It Helps :) – Gupta Dec 26 '15 at 07:04
  • I am working on has many through relationship with nested form.same as below link http://stackoverflow.com/questions/17607239/many-to-many-nested-attributes-in-rails-4-with-strong-parameters can you please help me to resolve this. Thanks – Jigar Bhatt Dec 29 '15 at 06:10
  • 1
    @JigarBhatt What error you are getting ? Or How might i could resolve your issue ? – Gupta Dec 29 '15 at 13:41