search for: after_initialize

Displaying 20 results from an estimated 50 matches for "after_initialize".

2010 Jun 11
2
AR after_initialize quandry
...where I am abstracting the creation of models. After a fair bit of indirection, I eventually get around to making a .new call. I need to be able to pass params to that .new call. I know I can''t use def initialize() in an AR model (rather frustrating). So, I have found some references to after_initialize, but that doesn''t work either. I see several threads on people trying to use after_initialize, but no conclusive answers to the same errors I am seeing -- undefined method `stringify_keys! Example refc: http://www.ruby-forum.com/topic/86173 My after_initialize is just this simple code:...
2006 Oct 27
5
Purpose of after_initialize in ActiveRecord?
All, I have a quick question - is the purpose of implementing after_initialize to allow custom attribute initialization for AR descendants? I have been overriding initialize whenever I require custom setup of my model classes, like so: def initialize(attributes = nil) super self.Status = ''G'' end So I have a couple of questions: 1) Could I a...
2008 Aug 24
2
config.after_initialize and development mode
Hi, I''m using activemerchant and setting up a class variable using config.after_initialize. It works great for the first request, but then the variable is nil. config.after_initialize do ActiveMerchant::Billing::Base.mode = :test OrderTransaction.gateway = ActiveMerchant::Billing::PaypalGateway.new(...) end I''m assuming this is is due to the model reloading after each req...
2010 Jan 12
1
Rails after_initialize block doesn't seem to be run in the backgroundrb context
Hi all, Just had an interesting production failure while updating my Rails application - a bunch of code previously in config/environment.rb was put into config/initializers. That resulted in me having to wrap some code in config/environments/production.rb into an after_initialize do ... end block When backgroundrb was run against that code, the setup done in that after_initialize block wasn''t run, leading to a failure in several background jobs that required it. Two purposes for the email - firstly, a heads-up that this is a problem, in case anyone else ends up b...
2006 Jul 06
2
Custom init. of ActiveRecord objects - best practices
All, I''m wanting to initialize an ActiveRecord object. I understand that there is a method called after_initialize that appears to get called right after the ActiveRecord object is instantiated. Is after_initialize a Ruby thing or a Rails thing? Where is after_initialize documented? Can I pass parameters to it? If so, how? I want to initialize the "belongs_to" attribute of my new ActiveRecord obj...
2006 Aug 11
1
after_initialize and after_save
Hello, I am still reading Agile Web Developer pdf, it says about after_initialize and after_find. There is a Joe asks part when I read that they are special, however I didn''t really get the idea, why they are. On rubyonrails.org in the manual there is some sort description about their speciality, however their full descriptions are missing. What are the difference b...
2007 Jun 16
1
extending activerecord base with after_initialize
hi, i am having problems getting after_initialize for activerecord subclasses. basically, i am trying to use the guid plugin by andy singleton. i am having problems extending the class to the plugins i installed, so i figured i will just apply this to all the models in my project. i trled to add the following to my lib/ folder. my goal is to e...
2006 May 24
1
Observer behavior differences between DEV and TEST environments ?
...pp/models/greenback_transaction_observer.rb, inherited from AR::Observer, defined methods, registered in config/environment.rb, but things don''t work... So, I took a step back and put in the following code: class GreenbackTransactionObserver < ActiveRecord::Observer %w( after_find after_initialize before_save after_save before_create after_create before_update after_update before_validation after_validation before_validation_on_create after_validation_on_create before_validation_on_update after_validation_on_update before_destroy after_destroy ).each do |method| define_me...
2006 Aug 09
7
function before_save
Hi everybody I would like a function as the "before_save" method in the model. But it must be the opposite. When I take out data from the database through the model, I want to call a function before the data are available in the controller. Can anyone help me? ---- >>>> thx <<<< ---- -- Posted via http://www.ruby-forum.com/.
2009 Jun 24
1
accepts_nested_attributes_for :reject_if issue
...Record::Base belongs_to :billing_address belongs_to :shipping_address accepts_nested_attributes_for :billing_address accepts_nested_attributes_for :shipping_address, :reject_if => proc { |attributes| attributes[''has_shipping_address''] != ''1'' } def after_initialize self.build_billing_address unless billing_address self.build_shipping_address unless shipping_address end end class ShippingAddress < OrderAddress attr_accessor :has_shipping_address end class OrderAddress < ActiveRecord::Base validates_presence_of :name #more validations h...
2011 Jun 11
15
after initialize blowing up---help
...lf.up create_table :greetings do |t| t.string :greet t.string :language t.integer :count t.timestamps end end def self.down drop_table :greetings end end ================== greeting.rb ============================= class Greeting < ActiveRecord::Base after_initialize :check_greet validates_uniqueness_of :count def check_greet self.language = lanugage.capitalize end end =================== greeting.yml ========================== hello: greet: howdy language: english count: 44 hola: greet: hola, que tal language: spanish count: 55 =======...
2011 Apr 27
3
Can't configure cach_store in an initializer
I''m trying to use redis-store as my cache_store. I also have an app_confirg.rb initalizer which loads a yaml file for config settings. In my redis.rb initializer I have: MyApp::Application.config.cache_store = :redis_store, APP_CONFIG[''redis''] However, this doesn''t appear to work. If I do Rails.cache in my rails console I can clearly see it''s using
2006 Jun 26
1
ActiveRecord, instance_eval, and PStore
I''m trying to add some per-object behaviors to objects descended from ActiveRecord, by using after_initialize() and instance_eval(), like this: def after_initialize self.instance_eval(File.read(''customers/default.rb'')) end (Ultimately the file path will be dynamic, this is just proof of concept.) default.rb just contains a few method definitions like this one: def support_phone_num...
2006 Jul 11
2
extending an active record model with obj.extend(Module)
Hi everyone, I am trying to extend an active record model using obj.extend(module). The aim is to overwrite some of the model methods depending on what''s in the object. For example, the base class would be "file", with objects such as "image", and "text". The aim is to be able to something like this: file = File.new() file.extend(Image) file.process
2011 Feb 04
6
A plugin inside an engine
In Rails 3, is it possible to have a plugin inside an engine? Adding the lib dir of the plugin to autoload_paths doesn''t seem to work. I can manage to load it by adding that path to $: and requiring the init.rb file, but I''m not sure that''s the correct approach. Thanks! -- You received this message because you are subscribed to the Google Groups "Ruby on Rails:
2007 Apr 24
2
Passing environment to mongrel apps
...looking for a way to pass environment variables to my Rails app via Mongrel. I want to conditionally execute code in environment.rb based on these environment variables. Background: 1. I have some special stuff I want to do when my Rails app first starts 2. So, stuff it in environment.rb in after_initialize, right? 3. Not this time. I have several other background processes that include environment.rb, but shouldn''t run this code. Some other background: I''m using lighttpd, mongrel, and mongrel_cluster If there''s not a way to do this, I can fallback on having all the...
2007 Jul 25
5
set_default_values rocket science - continuing after_initialize/after_find misfeature
Just starting a new thread so that it''s easy to follow. I just submitted a patch at trac to add a new class method called "set_default_values" ( not a great name I think ), which lets you do all kinds of crazy stuff with setting default values. This ticket is at http://dev.rubyonrails.org/ticket/9093 and example pastie is at http://pastie.caboo.se/81925 It also lets you make
2006 Jun 21
3
[PLUG] new plugin, acts_as_modified
Nothing special, just my first plugin. it came out of a request I saw on the list from someone who wanted to be able to tell if a model''s attributes had changed prior to being saved. Well, this plugin does just that (at least I hope it does). http://rubyforge.org/projects/actsasmodified/ Please see the README for details. Constructive comments and criticisms are appreciated. Chris
2006 Feb 24
3
Migrations, data loads and changes to the model
...bjects of Person to do so, and the third creates a new table, "cars", and modifies the "persons" table to create a :has_one relationship of Person[1]->[1]Car. Let''s also say I want all Persons to have at least one car, so I change my Person model to have: def after_initialize self.car = Car.new unless self.car end Now, what happens, is that when I issue a "rake migrate" on my intial database When I start from scratch (new database) and do a rake migrate, my data load fails because it uses the model class Person, yet Person''s code now has an a...
2010 Nov 09
2
AMQP and Unicorn (mq gem)
...e :none case? It used to be empty and return nil. Now it has the same behavior as the non- evented :standard server. The problem is that the "server_spy" wasn''t recognizing Unicorn. So far so good. Originally, the AMQP/Qusion conf goes into config/environment.rb config.after_initialize do Qusion.start(:user => ''guest'',:pass => ''mypass'') end However, when I publish messages to the queue, it just isn''t posted. It silently fails. I''ve then tried to put this on config/unicorn.rb: before_fork do |server,w...