Re: Rails, insert into 3 tables from 1 controller

Colin Law
Mon, 04 Apr 2011 08:34:34 -0000

On 4 April 2011 08:56, HelloRails
<vitalizakharoff-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:
> 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                                    # :(

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

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

--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.