I need a way to set a counter that will show how many of the model have been created and put this in each instance. This will allow me to use the record number as an input to another field before id is set. After things are settled, the real id number can be substituted for the counter. I''ve tried new in the controller, but that''s for a create action from the app. Thanks Bob -- 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.
Colin Law
2012-Feb-08 09:39 UTC
Re: Looking for a way to set a variable when doing Model.new
On 8 February 2012 07:12, Bob Smith <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I need a way to set a counter that will show how many of the model > have been created and put this in each instance. This will allow me to > use the record number as an input to another field before id is set. > After things are settled, the real id number can be substituted for > the counter.If you do that and two users come along at the same time and make new objects then they will both get the same number. Can you not just use a before_save filter to set the field? If you think not can you explain what you are trying to do (and why) in more detail as there is almost certainly a better solution. 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.
grentis
2012-Feb-08 11:05 UTC
Re: Looking for a way to set a variable when doing Model.new
You can use also after_initialize checking if the counter is already setted after_initialize :set_counter def set_counter self.counter ||= ModelName.all.count end -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/2x5_bSOJC0wJ. 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.
Michael Pavling
2012-Feb-08 11:11 UTC
Re: Re: Looking for a way to set a variable when doing Model.new
On 8 February 2012 11:05, grentis <grentis-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ModelName.all.countPlease don''t *ever* do this. If you want a count of all the objects in a table, use "ModeName.count". Using ".all.count" will instantiate an array of all of the objects just to do a count of the array... this could be very slow with a big table. -- 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.
grentis
2012-Feb-08 11:14 UTC
Re: Re: Looking for a way to set a variable when doing Model.new
Ops, sorry...I don''t know why I add .all :( -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/eOhjjXJLtU8J. 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.
Valery Kvon
2012-Feb-08 11:32 UTC
Re: Looking for a way to set a variable when doing Model.new
On 08.02.2012, at 13:39, Colin Law wrote:> On 8 February 2012 07:12, Bob Smith <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I need a way to set a counter that will show how many of the model >> have been created and put this in each instance. This will allow me to >> use the record number as an input to another field before id is set. >> After things are settled, the real id number can be substituted for >> the counter. > > If you do that and two users come along at the same time and make new > objects then they will both get the same number. Can you not just use > a before_save filter to set the field? If you think not can you > explain what you are trying to do (and why) in more detail as there is > almost certainly a better solution.I think actually he asked about something counter-caching solution for stand-alone model/table. Good question, for the best approach it has to be realized on a database level. The second approach is to build additional caching table to keep counter values for anything you want. The last one is not so elegant. -- 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.
Valery Kvon
2012-Feb-08 11:54 UTC
Re: Looking for a way to set a variable when doing Model.new
On 08.02.2012, at 15:05, grentis wrote:> You can use also after_initialize checking if the counter is already setted > > after_initialize :set_counter > > def set_counter > self.counter ||= ModelName.all.count > endImagine ''create'' action. During the period from the instance is initialized: @user = User.new(params[:user]) to the moment it saved @user.save anyone else can create user object(s), so your @user will be saved with incorrect counter value. -- 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.
Peter Vandenabeele
2012-Feb-08 12:11 UTC
Re: Looking for a way to set a variable when doing Model.new
On Wed, Feb 8, 2012 at 10:39 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 8 February 2012 07:12, Bob Smith <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I need a way to set a counter that will show how many of the model > > have been created and put this in each instance. This will allow me to > > use the record number as an input to another field before id is set. > > After things are settled, the real id number can be substituted for > > the counter. > > If you do that and two users come along at the same time and make new > objects then they will both get the same number. Can you not just use > a before_save filter to set the field? If you think not can you > explain what you are trying to do (and why) in more detail as there is > almost certainly a better solution. >Maybe the OP really wants a "sequence" (a guaranteed unique number that is more or less monotomically going up). The OP may be used to the concept that the primary ID of a record upon creation is such a sequence. But maybe he needs that number _before_ the record is saved. (e.g. to set a serial number that is saved in the record). If that is the business requirement, 3 solutions: 1) don''t do it that way. Use a separate sequence generator (which exists in Postgresql and probably in all databases) and use that to calculate the unique and more or less monoticly increasing serial number. Do NOT couple that "business id" with the actual internal database id. 2) If you want to use the database id, then you could use an external sequence generator and set the Object.id manually before saving (than configure your db in a way that the primary key is set from the application). 3) do an after_create (create the object with most data filled in, the create will generate the database id and in the after_create calculate the other field (e.g. serial number) and save again ... this is then an "update" so the "after_create" will not be triggered again). You may need some more tricks to make sure this "serial number" field cannot be modified, otherwise it could be out of sync with you internal database id (that is "back to (1) above" don''t link the "serial number" with the database id, just make it a separate column with a unique index and use a separate sequencer to generate those unique codes). HTH, Peter -- 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.
Bob Smith
2012-Feb-09 04:54 UTC
Re: Looking for a way to set a variable when doing Model.new
On Feb 8, 7:11 am, Peter Vandenabeele <pe...-jNuWw7i2w7syMbTcgqFhxg@public.gmane.org> wrote:> On Wed, Feb 8, 2012 at 10:39 AM, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > On 8 February 2012 07:12, Bob Smith <bsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I need a way to set a counter that will show how many of the model > > > have been created and put this in each instance. This will allow me to > > > use the record number as an input to another field before id is set. > > > After things are settled, the real id number can be substituted for > > > the counter. > > > If you do that and two users come along at the same time and make new > > objects then they will both get the same number. Can you not just use > > a before_save filter to set the field? If you think not can you > > explain what you are trying to do (and why) in more detail as there is > > almost certainly a better solution. > > Maybe the OP really wants a "sequence" (a guaranteed unique number > that is more or less monotomically going up). > > The OP may be used to the concept that the primary ID of a record upon > creation is such a sequence. But maybe he needs that number _before_ > the record is saved. (e.g. to set a serial number that is saved in the > record). > > If that is the business requirement, 3 solutions: > > 1) don''t do it that way. Use a separate sequence generator (which exists > in Postgresql and probably in all databases) and use that to calculate > the unique and more or less monoticly increasing serial number. Do NOT > couple that "business id" with the actual internal database id. > > 2) If you want to use the database id, then you could use an external > sequence generator and set the Object.id manually before saving (than > configure your db in a way that the primary key is set from the > application). > > 3) do an after_create (create the object with most data filled in, the > create will generate the database id and in the after_create calculate > the other field (e.g. serial number) and save again ... this is then an > "update" so the "after_create" will not be triggered again). You may > need some more tricks to make sure this "serial number" field cannot > be modified, otherwise it could be out of sync with you internal database > id (that is "back to (1) above" don''t link the "serial number" with the > database id, just make it a separate column with a unique index and > use a separate sequencer to generate those unique codes). > > HTH, > > PeterIt seems a bit of explanation is in order.. after_create, after_initialize, and the database won''t help me.. I need this value to be available as soon as new creates a new object in the view, before any saving. I have the logic set to do this with existing records. What I am trying to do is have a unique value set in each new record so that they can be part of a radio button list. This is how the new objects are created <%= link_to_function "Add a Person" do |page| page.insert_html :bottom, ''empty'', :partial => ''shared/ person'', :locals => {:temp => @temp,:household => @household}, :object => Person.new end %> My app is used at the signup table of a foodbank. My app has a household record that has address, zip, phone, etc. This has a child model called people with.. guess what... people :> There are radio buttons referencing the record id next to each person where the head of household can be selected. This is where the problem lies. No problem with existing people, but new ones have no id yet. I have added a variable to the people model to hold a counter. I have the logic to look back at the record after save, find the id of the wanted record, and replace the counter with the correct value. Now all I need is a way to set the counter at each new record. Hope this makes it clearer. Thanks Bob -- 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.
Bob Smith
2012-Feb-09 05:48 UTC
Re: Looking for a way to set a variable when doing Model.new
On Feb 8, 11:54 pm, Bob Smith <bsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 8, 7:11 am, Peter Vandenabeele <pe...-jNuWw7i2w7syMbTcgqFhxg@public.gmane.org> wrote: > > > > > On Wed, Feb 8, 2012 at 10:39 AM, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > On 8 February 2012 07:12, Bob Smith <bsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I need a way to set a counter that will show how many of the model > > > > have been created and put this in each instance. This will allow me to > > > > use the record number as an input to another field before id is set. > > > > After things are settled, the real id number can be substituted for > > > > the counter. > > > > If you do that and two users come along at the same time and make new > > > objects then they will both get the same number. Can you not just use > > > a before_save filter to set the field? If you think not can you > > > explain what you are trying to do (and why) in more detail as there is > > > almost certainly a better solution. > > > Maybe the OP really wants a "sequence" (a guaranteed unique number > > that is more or less monotomically going up). > > > The OP may be used to the concept that the primary ID of a record upon > > creation is such a sequence. But maybe he needs that number _before_ > > the record is saved. (e.g. to set a serial number that is saved in the > > record). > > > If that is the business requirement, 3 solutions: > > > 1) don''t do it that way. Use a separate sequence generator (which exists > > in Postgresql and probably in all databases) and use that to calculate > > the unique and more or less monoticly increasing serial number. Do NOT > > couple that "business id" with the actual internal database id. > > > 2) If you want to use the database id, then you could use an external > > sequence generator and set the Object.id manually before saving (than > > configure your db in a way that the primary key is set from the > > application). > > > 3) do an after_create (create the object with most data filled in, the > > create will generate the database id and in the after_create calculate > > the other field (e.g. serial number) and save again ... this is then an > > "update" so the "after_create" will not be triggered again). You may > > need some more tricks to make sure this "serial number" field cannot > > be modified, otherwise it could be out of sync with you internal database > > id (that is "back to (1) above" don''t link the "serial number" with the > > database id, just make it a separate column with a unique index and > > use a separate sequencer to generate those unique codes). > > > HTH, > > > Peter > > It seems a bit of explanation is in order.. > > after_create, after_initialize, and the database won''t help me.. I > need this value to be available as soon as new creates a new object in > the view, before any saving. I have the logic set to do this with > existing records. What I am trying to do is have a unique value set in > each new record so that they can be part of a radio button list. > > This is how the new objects are created > > <%= link_to_function "Add a Person" do |page| > page.insert_html :bottom, ''empty'', :partial => ''shared/ > person'', :locals => {:temp => @temp,:household => @household}, :object > => Person.new > end %> > > My app is used at the signup table of a foodbank. My app has a > household record that has address, zip, phone, etc. This has a child > model called people with.. guess what... people :> There are radio > buttons referencing the record id next to each person where the head > of household can be selected. This is where the problem lies. No > problem with existing people, but new ones have no id yet. I have > added a variable to the people model to hold a counter. I have the > logic to look back at the record after save, find the id of the wanted > record, and replace the counter with the correct value. Now all I need > is a way to set the counter at each new record. Hope this makes it > clearer. > > Thanks > BobJust ALMOST fixed this thanks to google.. in person.rb def initialize(params = nil) super() self.hoh = $hoh $hoh = $hoh - 1 end This will set the model to the value in $hoh and decrement it once, but after that the $hoh = $hoh - 1 won''t execute. If I make more than 1 record, all values are the same.. Please help!! Thanks Bob -- 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.
Hassan Schroeder
2012-Feb-09 06:07 UTC
Re: Re: Looking for a way to set a variable when doing Model.new
On Wed, Feb 8, 2012 at 8:54 PM, Bob Smith <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> What I am trying to do is have a *unique value* set in each new recordSee: http://rubygems.org/gems/uuid HTH! -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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.
Peter Vandenabeele
2012-Feb-09 08:52 UTC
Re: Re: Looking for a way to set a variable when doing Model.new
On Thu, Feb 9, 2012 at 7:07 AM, Hassan Schroeder <hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, Feb 8, 2012 at 8:54 PM, Bob Smith <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 Vandenabeele http://twitter.com/peter_v http://rails.vandenabeele.com http://coderwall.com/peter_v -- 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.
Bob Smith
2012-Feb-11 19:33 UTC
Re: Looking for a way to set a variable when doing Model.new
On Feb 9, 3:52 am, Peter Vandenabeele <pe...-jNuWw7i2w7syMbTcgqFhxg@public.gmane.org> wrote:> On Thu, Feb 9, 2012 at 7:07 AM, Hassan Schroeder <hassan.schroe...@gmail.com > > > wrote: > > On Wed, Feb 8, 2012 at 8:54 PM, Bob Smith <bsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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://coderwall.com/peter_vI 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 -- 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.
Bob Smith
2012-Feb-26 21:32 UTC
Re: Looking for a way to set a variable when doing Model.new
On Feb 11, 2:33 pm, Bob Smith <bsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 9, 3:52 am, Peter Vandenabeele <pe...-jNuWw7i2w7syMbTcgqFhxg@public.gmane.org> wrote: > > > > > On Thu, Feb 9,2012at 7:07 AM, Hassan Schroeder <hassan.schroe...@gmail.com > > > > wrote: > > > On Wed, Feb 8,2012at 8:54 PM, Bob Smith <bsm...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > BobFinally 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 <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> -- 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.
Michael Pavling
2012-Feb-26 21:52 UTC
Re: Re: Looking for a way to set a variable when doing Model.new
On 26 February 2012 21:32, Bob Smith <bsm2th-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Finally found it. Here it is for anyone else in similar trouble. > > 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 > endGlobal variables? For the love of all that''s OO, at least hide it behind a singleton :-/ *shakes head* -- 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.