On Miércoles, 17 de Junio de 2009 16:04:38 cntrytwist escribió: > I appreciate your reply, but your method does not always work. > If there has been a newly created associated record, the > before_update_save does not know about the new associated record until > after the associated record is created. The associated record seems > to be created sometime after the before_update_save is run.
The best way is to use rails callbacks, before_create, before_update, before_save (and after counterparts). before_create/update_save and after_create/update_save should be used to set some attributes which you can't set in the model (because you need check something in request's parameters or set some values from session). It's the same you should make in your controller if you write the controller without ActiveScaffold Anyway, associated record are created and assigned to record in update_record_from_params, which is called before the before_update_save method. So you can access to newly created associated records in before_update_save, although they don't have id. > We are > also talking here about a through relationship where I'm working with > the join table and setting an attribute on the join table, and setting > the associated record to the join table. It would just be far more > simple and easier for ActiveScaffold to run the AS call backs on the > associated model. Also, the code I want to run has nothing to do with > the join table, but is specific to the associated record its self. Probably you can't use the through association because maybe it tries to find the records using the id. I haven't used through associations too times, and I have never used them with ActiveScaffold. > The solution I found yesterday is to look at the params. If there is > record[association][id] parameter, then I assume new association > record. I set a session variable that gets checked in the > after_update_save. I'm still having trouble with this method actually > saving the associated record, but it is about the only logical > alternative at this point. > I should also mention that I am running rails 2.3.2 > Any further thoughts on this? > Thanks a million. Maybe you should put an example, with your models and relationships. It's too hard understand it without real models, but it shouldnt' be necessary to run callbacks in associated models because you must be able to get associated records from record variable. > > On Jun 16, 5:53 pm, "G. Sobrinho" <[email protected]> wrote: > > You can use before_update_save from top controller and call rails methods > > to get associated records. I used it on my application: > > def before_save_create(record) > > record.users.each do |user| > > user.role_id = x > > end > > end > > > > But if you have to call the associated controller method, i think you > > need to create a static method on associated controller which you call it > > and before_update_save on that controller also. > > > > 2009/6/16 cntrytwist <[email protected]> > > > > > It appears that ActiveScaffold does not call the before_update_save/ > > > before_create_save callbacks for associations. > > > What is the best way to make this happen? > > > I have an association in which I need to set some fields on the > > > associated record when it is created. The issue is that I don't know > > > how to tell if the associated record is new or if it is just being > > > updated. If ActiveRecord would issue the callbacks for the associated > > > records, I'd be just fine. > > > Please advise me of the best way to handle this. > > > Thanks in advance. > > > > -- > > Regards, > > > > Gabriel Sobrinho > > E-mail: [email protected] > > Phone: +55 31 8775 8378 > > > > Don't print this e-mail. The nature is thankful and your money also. > > -- Sergio Cambra .:: entreCables S.L. ::. Nicolás Guillén 6, locales 2 y 3. 50.018 Zaragoza T) 902 021 404 F) 976 52 98 07 E) [email protected] --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "ActiveScaffold : Ruby on Rails plugin" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/activescaffold?hl=en -~----------~----~----~----~------~----~------~--~---
