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? -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Without knowing how your models(Question, Token, Email) are linked by relations, it''s difficult to advice you. Could you please post it? Thks On Apr 4, 9:56 am, HelloRails <vitalizakhar...-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 # :( > end > end > > render :json => @question.to_ext_json(:success => success) > end > > How can i fix this?-- 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. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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... -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 4 April 2011 09:21, vone vonich <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> 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 :questionand belongs_to :token presumably. Colin> > validates_presence_of :text > validates_uniqueness_of :question_id , :scope=>"token", :message => > "ERROR!" > > 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. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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). -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 4 April 2011 09:46, HelloRails <vitalizakharoff-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.