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
if success = @question.save
emails = Email.find(:all) #this pasrt
emails.each do |e| #doesnt work
Token.create(:token=>@w,:is_active=>"1")
end #
end
end
render :json => @question.to_ext_json(:success => success)
end
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
if success = @question.save
emails = Email.find(:all) #this pasrt
emails.each do |e| #doesnt work
Token.create(:token=>@w,:is_active=>"1")
end #
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.
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?