I have a User model instance which contains an array of Item model
instances related via user_id in the Item model. I''ve noticed that
when i find a set of Item records and assign that to the items array
within the User object, AR updates those records and sets the user_id
field to that of the instantiated User object. This is fine and all,
but I need to update additional fields when the Item records are
assigned, and this results in extraneous updates in addition to issues
with optimistic locking. sample code below:
class User < ActiveRecord::Base
has_many :items
def get_items
items.clear
# rows apparently get updated to reflect user ownship automatically by AR
# when appending, due to model relationship (i assume)
items << Item.get_items
end
end
class Item < ActiveRecord::Base
belongs_to :user
def self.get_items
find_by_user_id(nil)
end
end
I have a problem with this in that I need to update another field (a
status field) that reflects the status of the now assigned record. so
there are x*2 updates...one automatically done by AR and one by the
code to update the status field.
Is there any way I can tell AR to either 1) not do the automatic
update and I can handle it in the code, or 2) have AR update the
status field in addition to the user_id field when it appends the item
records to the User.items array?
in addition, i''ve noticed that if you delete an item from the array
(Array.delete), AR will update the record in the db and set the
user_id to NULL. I prefer this not to happen either.
Any help would be appreciated
Chris
Chris Hall wrote:> Is there any way I can tell AR to either 1) not do the automatic > update and I can handle it in the code, or 2) have AR update the > status field in addition to the user_id field when it appends the item > records to the User.items array?Will this do what you want? def get_items items.clear Item.get_items.each do |item| item.status = ... items << item end end end> in addition, i''ve noticed that if you delete an item from the array > (Array.delete), AR will update the record in the db and set the > user_id to NULL. I prefer this not to happen either.Use items.to_ary.delete. Of course when doing this the deleted items will be back when the association gets reloaded. -- We develop, watch us RoR, in numbers too big to ignore.
actually, after reading through the rails book, i am seeing that has_many adds several methods to the object. collection(force_reload = false) collection<<(object, …) collection.push_with_attributes(object, join_attributes) collection.delete(object, …) collection=objects collection_singular_ids=ids collection.clear collection.empty? collection.size collection.find(id) where "collection" is replaced by the object symbol, so in my example, these would become items(...) items<< items.delete ... and so on so now i guess my question is, how would i go about overriding these methods...? On 10/11/05, Mark Reginald James <mrj@bigpond.net.au> wrote:> Chris Hall wrote: > > > Is there any way I can tell AR to either 1) not do the automatic > > update and I can handle it in the code, or 2) have AR update the > > status field in addition to the user_id field when it appends the item > > records to the User.items array? > > Will this do what you want? > > def get_items > items.clear > Item.get_items.each do |item| > item.status = ... > items << item > end > end > end > > > in addition, i've noticed that if you delete an item from the array > > (Array.delete), AR will update the record in the db and set the > > user_id to NULL. I prefer this not to happen either. > > Use items.to_ary.delete. Of course when doing this the deleted items > will be back when the association gets reloaded. > > -- > We develop, watch us RoR, in numbers too big to ignore. > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails