Rails, insert into 3 tables from 1 controller

Hi.

I have 3 tables: questions, tokens, email. When I paste question, some token (rand(9999) for example) inserting, too. Its great. But i need paste tokens as much as I have emails in my the database called 'Email' (i have about ~10).

I think i should use each.do for email, but how, following code doesnt work

#controller

  def create     @w = Digest::SHA1.hexdigest(rand(99999999).to_s)

    @question = Question.new(params[:question])

    if success = @question.save            emails = Email.find(:all) #this pasrt            emails.each do |e| #doesnt work                    Token.create(:token=>@w,:is_active=>"1")                end # :frowning:            end     end

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

How can i fix this?

Without knowing how your models(Question, Token, Email) are linked by relations, it's difficult to advice you. Could you please post it? Thks

Of course, there are my models (did you mean this , yes?)

#question model class Question < ActiveRecord::Base   has_many :answers end

#token model class Token < ActiveRecord::Base   has_one :answer end

#email model class Email < ActiveRecord::Base end

I forgot about answer model, but its not necessary, but i pasted it too for better understanding my system.

#answer model class Answer < ActiveRecord::Base belongs_to :question

validates_presence_of :text validates_uniqueness_of :question_id , :scope=>"token", :message => "ERROR!"

end

So, this is...

Hi.

I have 3 tables: questions, tokens, email. When I paste question, some token (rand(9999) for example) inserting, too. Its great. But i need paste tokens as much as I have emails in my the database called 'Email' (i have about ~10).

I think i should use each.do for email, but how, following code doesnt work

#controller

def create @w = Digest::SHA1.hexdigest(rand(99999999).to_s)

@question = Question.new(params[:question])

if success = @question.save emails = Email.find(:all) #this pasrt emails.each do |e| #doesnt work Token.create(:token=>@w,:is_active=>"1") end # :frowning:

What do you mean by 'it doesn't work'? So every time you save a question you make new Token records, as many as there are email records, though all the Token records will be identical (as they have no relationship to the email). That seems an odd thing to do. Have a look at the Rails Guide on debugging, then you can find out how to use ruby-debug to break into your code and inspect data and follow the flow to see what the problem is.

Colin

I forgot about answer model, but its not necessary, but i pasted it too for better understanding my system.

#answer model class Answer < ActiveRecord::Base belongs_to :question

and belongs_to :token presumably.

Colin

And my email model has_many: tokens ?, right?

Okei, i added belongs_to .. in my answer model.

I mean doesn't work.. questions is added successfully, but in token models i dont have any tokens (I should have about 10).

Please don't top post, it makes it difficult to follow the thread. Insert your reply at appropriate points in previous message. Thanks.

And my email model has_many: tokens ?, right?

I don't know, that is for you to say. If an email has many tokens then token belongs to email and you need an email_id in tokens table. You do not appear to be setting that when you create each token.

Okei, i added belongs_to .. in my answer model.

I mean doesn't work.. questions is added successfully, but in token models i dont have any tokens (I should have about 10).

Have you got any validations in Token that would prevent them being saved? When you used ruby-debug to break in before the create does the code get there ok?

Colin