Tuesday, April 5, 2011

[Rails] Re: trying to seed data from seeds.rb within a class structure

One way to do what you want would be (assuming Title and Body will always contain the same number of elements):

  def posts!
    Title.each_with_index do |t, i|
      post! t, Body[i], @current_user.id
    end
  end
 
I'm sure there are more clever, ruby-ish ways for parallel iteration that using #each_with_index.

Okay, I couldn't help myself. Here's another alternative/example for this kind of "parallel iteration"

fruits = %w{ apples pineapples oranges }
states = %w{ Washington Hawaii Florida }

states.zip(fruits) do |state, fruit|
  puts "#{state} grows #{fruit}"
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@googlegroups.com.
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate