Vitali Zakharoff
Vitali Zakharoff

Reputation: 353

Database + Rails + insert data into 3 tables

I have following code. Damn, I stuck with this, I know that is very easy...

  def create
      @question = Question.create(:text => params[:question][:text], :security_token => "test"))

    if success = @question.save
           respondents = Respondent.find(:all)
           respondents.each do |res|
                Inquiry.create(:question_id=>res.question.id.to_i, :respondent_id=>res.id.to_i)
           end
    end

      render :json => @question.to_ext_json(:success => success)
    end

As you see, I have 3 tables => questions (id, text, security_token), respondents (id, email) and relation-table called inquiry (id, questiond_id, respondent_id). Before I start i told that I have 3 record in my email table. I want do following: When I add question, it also watching how many email i have in my tables (now i have 3 as i said) and add info in inqury table. for example: I NEED FOLLOWING IN MY INQUIRY TABLE (after I have added question):

id  | questiond_id | respondent_id
1   |     2                  |   1
2   |     2                  |   2
3   |     2                  |  3

How i can do it? I using each do and check how many email i have, but NOW its doesnt work, dont know why, what i did wrong there in my code?

Upvotes: 2

Views: 1743

Answers (3)

Vitali Zakharoff
Vitali Zakharoff

Reputation: 353

Now question don't want to be edited :(

my models looks:

question model

class Question < ActiveRecord::Base
  has_one :answer, :through => :inquiries #, :dependent => :destroy
  belongs_to :inquiry #, :through => :enquiry

  validates_presence_of :text, :message => "ERROR!"
end

inquiry model

class Inquiry < ActiveRecord::Base
  belongs_to :question
  belongs_to :respondent
  has_one    :answer
end

respondent

class Respondent < ActiveRecord::Base
  has_many :inquiries
  has_many :questions, :through => :inquiries
  has_many :answers,   :through => :inquiries
end

Maybe something with models?

Upvotes: 0

Sector
Sector

Reputation: 1210

Maybe this?

Inquiry.create(:question_id=>@question.id.to_i, :respondent_id=>res.id.to_i)

Upvotes: 2

Vitali Zakharoff
Vitali Zakharoff

Reputation: 353

This controller I have in my question_controller. Its ok. Added question in table called questions.

 def create
      @question = Question.create(:text => params[:question][:text], :security_token => "test"))

      render :json => @question.to_ext_json(:success => @question.save)
    end

Next, I have respondents table ( where I have ~3 record). Ok? Ok! Clear!

And I have 3rd table named inquiries. This is relationship table with: id, question_id and respondent_id. Ok? Ok. Clear!

I want do following: When I adding question, it also adding info (question_id & respondent_id) in inquiries table. So if i have 3 record in my email table , I should get 3 record in my inquiries table, right? Right.

I adding following code in question_cotnroller

     def create
          @question = Question.create(:text => params[:question][:text], :security_token => "test"))
if success = @question.save
       respondents = Respondent.find(:all)  #find all my respondent email
       respondents.each do |res|            # each do 
            Inquiry.create(:question_id=>res.question.id.to_i, :respondent_id=>res.id.to_i)        #adding inti inquiries table.. BUT DOESNT:( WHY?
       end
end    
          render :json => @question.to_ext_json(:success => success)
        end

SO.. question is following, there are problem with this code: ...respondents.each do |res|... , i have bad code there.

Upvotes: 0

Related Questions