Displaying 20 results from an estimated 6000 matches similar to: "Custom init. of ActiveRecord objects - best practices"
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 achieve the same
2006 Jul 11
4
checkboxes with a has_many :through relation
All,
I''m populating a view with checkboxes in a list. I want to be able to
assign to my has_many relationship using the post values of these
checkboxes. Essentially, I want to populate the relationship table
which will create the correct join values in my model.
Here is my check_box_tag code:
<%= check_box_tag("@current_job[target_list_ids][]",
2006 Jul 06
4
ERb question: Embedding <%= %> in helper method calls
I had a situation where I wanted to use a <%= %> (scriptlet) in my
text_field call to set the "disabled" attribute on the text field, like
so:
<%= text_field(:current_job, ''removeLinkPos'', :value => '''', :id =>
''offset'', :maxlength => 2, :disabled => <%= sometest ? ''true'' :
2006 Jul 18
1
Auto-populating time portion of a datetime field from a form
I see that if I use the date_select helper in my view, then in the
generated page, my selects are named obj[datetime_field(1i)] (and 2i and
3i for the other parts). These auto-populate the date portion of my
datetime_field.
I have another datetime field that I''m presenting the time from and I
would like to use a similar scheme to autopopulate it. Currently, I''ve
used the
2006 Jul 06
3
Understanding date_select naming conventions
I am using the date_select helper to generate date select elements in a
form.
I notice that the generated HTML shows that these selects have the
following names:
Month: current_job[StartDate(2i)]
Day: current_job[StartDate(3i)]
Year: current_job[StartDate(1i)]
I''m assuming that the Xi identifiers somehow specify the appropriate
date part from a date or time value.
Can anyone
2006 Jul 10
10
has_many :through and foreign key parameters
I just took my HABTM and turned it into a :through since my join table
has another "non-joiny" attribute.
I went from this:
has_many_and_belongs_to :jobs, :join_table => ''tablename'',
:foreign_key => ''x'',
:association_foreign_key => ''y''
to this:
has_many :jobs, :through =>
2008 Mar 19
5
object_transactions plugin w/2.x disables AR session store
All,
Win XP
Rails 2.0.2
object_transactions plugin
(http://code.bitsweat.net/svn/object_transactions/)
transaction-simple gem 1.4 (required by object_transactions plugin)
AR SQL Server Adapter 1.0
SQL Server 2000
Using the object_transactions plugin with Rails 2.0.2 (and I assume
based on what I''m seeing that this goes for Rails 2.x) disables the
ability to save to an ActiveRecord based
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 Jul 14
7
Form validation - keepin correct fields displayed on refresh
All,
I''m finally doing my first real form in Rails - the model object that
I''m entering information for has 8 validations so far.
If I type in good values for all the fields but one, I get the pretty
validation, and the nice field highlighting, but all of the fields are
cleared, forcing me to retype all of that info. That is a big drag.
Is there a standard way to get the
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
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
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/.
2006 May 24
1
Observer behavior differences between DEV and TEST environments ?
Hi all !
I''m having an issue and I can''t seem to make heads or tails of it.
I am attempting to add an observer to a model object. I created
app/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
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
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
2007 Aug 23
6
Calling a method on class creation
I have a model User that has property called status. Status is just a
string that can only be evaluated and not persisted. I.e. it doesn''t
have a field in the user table.
Anyways, I would like a method get_status to be called anytime I
create a new instance of User. For example:
user=User.new
Since I created a new instance of User, I should now have access to
the status property, e.g.:
2006 Jul 13
4
Does text_field go directly to attributes hash in AR object?
All,
I''m having a hell of a time figuring out what is going on here.
I''m trying to override one of my getters so that I can format it a
certain way in my form. But I can''t seem to get text_field to call the
appropriate method on my object.
So here''s my getter:
public
def FAX
fax = read_attribute(:FAX)
puts fax
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