On Feb 11, 2:33 pm, Bob Smith <[email protected]> wrote:
> On Feb 9, 3:52 am, Peter Vandenabeele <[email protected]> wrote:
>
>
>
> > On Thu, Feb 9,2012at 7:07 AM, Hassan Schroeder <[email protected]
>
> > > wrote:
> > > On Wed, Feb 8,2012at 8:54 PM, Bob Smith <[email protected]> wrote:
>
> > > > What I am trying to do is have a *unique value* set in each new record
>
> > > See:http://rubygems.org/gems/uuid
>
> > Indeed.
>
> > And maybe better to not mess with the `def initialize`, but use the
> > `after_initialize` function that is provided by Rails. I did it like this.
>
> > ../lib/uuid_helper.rb
>
> > require 'uuidtools'
>
> > module UUIDHelper
> >   extend ActiveSupport::Concern
>
> >   included do
> >     after_initialize :set_uuid
> >     def set_uuid
> >       unless uuid  # Note 1 below
> >         self.uuid = UUIDTools::UUID.random_create.to_s
> >       end
> >     end
> >   end
> > end
>
> > ../app/model/person.rb
>
> > class Person < ActiveRecord::Base
>
> >   # UUID
> >   include UUIDHelper
> >   ...
> > end
>
> > Note 1:
> > Be careful, it is a little tricky to _only_ set the value of the uuid
> > when it is not yet present, otherwise reading back the value
> > from the database will build a new instance of Person with a
> > new random uuid. The code above is well tested and works.
>
> > Maybe you will also need to add ../lib to your default load path.
> > In config/application.rb
>
> >     # Custom directories with classes and modules you want to be
> > autoloadable.
> >     config.autoload_paths += %W(#{config.root}/lib)
> >     config.autoload_paths += Dir["#{config.root}/lib/**/"]
>
> > HTH,
>
> > Peter
>
> > *** Available for a new project ***
>
> > Peter 
> > Vandenabeelehttp://twitter.com/peter_vhttp://rails.vandenabeele.comhttp://coderwa...
>
> I don't have a config/application.rb. Forgot to mention it, but I'm
> using 2.3.9 until I get rid of bugs, then the 3.0 project starts.. :>
> Can this be done for 2.3.9?
>
> Thanks
> Bob

Finally found it. Here it is for anyone else in similar trouble.

In the view

<%= link_to_remote "Add a Person", :url => "/people/add_person" %>

In the controller

  def add_person
    person = Person.new()
    person.hoh = $hoh
    $hoh = $hoh - 1
    render :update do |page|
page.insert_html :bottom, 'empty', :partial => 'shared/
person', :object => person
    end
  end

This will add a new object AND allow the play with the hoh in the new
model AND decrement it for the next new object.
As usual, everything is possible in Rails, once you find an allowed
way to do it,

Bob <[email protected]>

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to