Displaying 20 results from an estimated 3000 matches similar to: "Purpose of after_initialize in ActiveRecord?"
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
2010 Jun 11
2
AR after_initialize quandry
I have a case 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
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
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
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 extended the activerecord class so that it uses
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
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 between
2006 Jan 24
2
webrick and in_place_editor_field
I''m having a strange problem with webrick spinning forever. I have a
view ''list'' that renders a collection:
<%= render :partial => ''item'', :collection => @items %>
The item partial looks like so:
<div id="item_<%= item.id %>">
...
<div id="price">
<%= in_place_editor_field :item, :price
2006 Aug 06
2
help with method_missing in ActiveRecord
I am serializing a data object (FooData) into an ActiveRecord column (Foo).
I''d like to be able to do delegate methods to the data class if the Foo
class doesn''t have that attribute. So instead of:
f = Foo.new
f.data.item1
I''d like to do:
f = Foo.new
f.item1
I was hoping that I could add a method_missing method to my Foo class and
call methods in the FooData class
2006 Feb 07
3
in place edit, save on blur
I thought I''d seen some discussion on this, but can''t find it. I have
a text area that I''d like to save on blur unless the user clicks
cancel. Anyone got a nice example of this? It''s non-obvious to me
how I force the submit via javascript. Thanks,
pt.
--
Parker Thompson
http://www.parkert.com/
510.541.0125
2006 Feb 06
1
template choice error (layout.rb)
Hey,
I installed the rjs plugin yesterday and started using it for one set
of views. Today I notice that in an entirely separate
controller/template I''m seeing this error:
TypeError (can''t dup NilClass):
/vendor/plugins/javascript_generator_templates/lib/add_rjs_to_action_controller.rb:41:in
`dup''
2009 Jun 24
1
accepts_nested_attributes_for :reject_if issue
I have the following models:
class Order < ActiveRecord::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
2006 Jan 25
2
inserting column headings between grouped rows
Hello all,
I have what is conceptually a pretty simple poblem, but can''t think of
an obvious solution. I have a group of objects (@items), that I wish
to render as rows. Items have dates, and I wish to group them by that
date, inserting a header row each time the date changes e.g.:
header row
row 11/11/2004
row 11/11/2004
header row
row 11/23/2004
I''m currently
2006 May 10
2
Exploring ActiveRecord::Base#serialize
Hi
I was recently taught about the useful ActiveRecord::Base#serialize.
It''s very useful for persisting arbitrary objects through the regular
database. For example, say you want to store a Set of all the years a
Convention took place. You do:
class Convention < ActiveRecord::Base
serialize :years, Set
end
This effectively creates a Convention#years attribute, which is a set:
you
2011 Jun 11
15
after initialize blowing up---help
All,
Can anyone see what''s happening here?
================== migration =======================
class CreateGreetings < ActiveRecord::Migration
def self.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
2007 Apr 24
2
Passing environment to mongrel apps
Hi guys,
I''m 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
2006 Feb 24
3
Migrations, data loads and changes to the model
Guys,
I''ve run across an interesting scenario with my migrations, and I hope
you might share some insight.
Let''s say I have 3 migrations and two Models (Person and Car):
001_initial_schema.rb
002_data_load.rb
003_add_car_association.rb
The first creates the initial schema...the second loads default data
using model objects of Person to do so, and the third creates a new
2005 Aug 25
3
Active Record Object Dependency
I have three model objects:
1. Document
2. Section
3. Content
According to my requirements a Document must have at least one Section.
Similarly a Section must have at least one Content.
How can I add a Section when a Document is created and add a Content when a
Section is created?
My code:
class Document < ActiveRecord::Base
has_many :sections
end
class Section < ActiveRecord::Base
2010 Nov 09
2
AMQP and Unicorn (mq gem)
Hi all,
I''m having issues with Unicorn and connecting to
RabbitMQ using the tmm1-amqp gem.
I''ve tried lots of approaches.
The classic initializer with Thread.new { EM.run }
for the Rails app and even tried using
the Qusion library.
(https://github.com/danielsdeleo/qusion)
I''ve made a simple mod to Qusion for it to monkey patch
unicorn too.
The code looks
2006 Dec 08
19
HABTM join table quirk
I''ve been bringing up a good basica framework for applications at work.
To give decent security I''ve been implementing the Rails Recipie for
Authorization of function calls modeled on the book''s implementation.
(Changes for our environment were added..but are minor.)
Since I initially let Rails create the join table between two data
tables with a HABTM relationship....I