Rails, insert into 3 tables from 1 controller

HelloRails
Mon, 04 Apr 2011 07:56:46 -0000

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                                    # :(
           end
    end


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

How can i fix this?