0

In a Ruby on Rails app I'm working on I have a Model that belongs_to a User. The User can create many of these.

When a user goes to the new action, I want to prepopulate the values with the values from the last record they created. These can then be changed if desired and used to create a new record.

I'm assuming that in the new action of my controller, I can get the most recent record (using something like this).

Model.first(:order => "created_at DESC")

Once I have that, how can I use it to pre-populate the record I created with the new method?

John
  • 135
  • 2
  • 11

2 Answers2

0

Can't you just initialize the attr?

class Model < ActiveRecord::Base
  def initialize_some_attr
    Model.first(:order => "created_at DESC").some_attr
  end
end
nicholasklick
  • 1,212
  • 10
  • 14
  • Yeah, I guess I could do that, I was hoping there was a way to do it all at once. Like maybe just getting Model.first and clearing out the id or something. I'm new to Ruby and Ruby on Rails, so I could be missing something obvious. – John Jul 27 '10 at 20:01
  • Seems like it is doing it all at once the only thing you need to do is Model.create ?? – nicholasklick Jul 28 '10 at 03:49
0
r = Model.first(:order => "created_at DESC")
n = Model.new(r.attributes)
smnirven
  • 1,518
  • 1
  • 13
  • 15