On Miércoles, 22 de Septiembre de 2010 16:25:43 Michael Bahnmiller escribió:
> yes, main_store *is* a has_one :through relationship. What do you then
> suggest?
I suggested to you changing after_update_save, but you can keep your current
code and add this line when store is saved:
record.set_main_store_target(store)
>
> On 09/22/2010 08:19 AM, Sergio Cambra .:: entreCables S.L. ::. wrote:
> > On Miércoles, 22 de Septiembre de 2010 15:40:58 Michael Bahnmiller
escribió:
> >> Here you go:
> >>
> >> def after_update_save(record)
> >> #
> >> ------------------------------------------------------------------------
> >>--- --------- # This is JUST a band-aid. It's here because there is an
> >> issue with has_one :through
> >> # either in ActiveScaffold or in Rails itself. Once that
> >> issue is fixed, this may
> >> # be able to be removed
> >> #
> >> main_store_post = params[:record][:main_store].to_i || 0
> >>
> >> if !main_store_post.nil?&& main_store_post> 0
> >>
> >> # Create a new SyncConfig Store Record
> >> scs = SyncConfigurationStore.new
> >> scs.StoreId = main_store_post
> >> scs.SyncConfigurationId = record.Id
> >>
> >> if scs.save # (and try to save it)
> >>
> >> # Save the associated store as a Main Store
> >> store = Store.find main_store_post
> >> store.MainStore = 1
> >> if !store.save
> >>
> >> # Roll back!
> >> scs.delete
> >> record.errors.add(:base, 'The Main Store could
> >> not be set.')
> >> else
> >>
> >> # Find any orphaned Main Stores
> >> # ... We'll use a named_scope in order to save
> >> query power
> >> Store.named_scope :parent_store,
> >>
> >> :conditions => 'Store.MainStore = 1 AND
> >>
> >> Store.Id NOT IN (SELECT DISTINCT(StoreId) FROM SyncConfigurationStores
> >> WHERE StoreId IS NOT NULL)'
> >> s = Store.parent_store # Going to get an array
> >> here...
> >>
> >>
> >> # Make sure any exsting old Main Store(s)
> >> is(are) demoted...
> >> if s.respond_to?(:each)
> >>
> >> s.each { |s_|
> >> s_.MainStore = 0
> >> s_.save
> >> }
> >>
> >> # Also clear out any unused
> >> SyncConfigurationStores
> >> scs = SyncConfigurationStore.all
> >>
> >> :conditions => 'StoreId IS NULL'
> >>
> >> scs.each { |scs_|
> >> scs_.delete if
> >> scs_.respond_to?(:delete) }
> >> end
> >> end
> >> else
> >> record.errors.add(:base, 'The Main Store could not
> >> be saved.')
> >> end
> >> end
> >> #
> >> #
> >> ------------------------------------------------------------------------
> >>--- --------- end
> >
> > If has_one :through association isn't named main_store, change it below.
> >
> > Try with this:
> >
> > def after_update_save(record)
> >
> > #------------------------------------------------------------------------
> > # This is JUST a band-aid. It's here because there is an issue with
> > has_one :through
> > # either in ActiveScaffold or in Rails itself. Once that
> > issue is fixed, this may
> > # be able to be removed
> > #
> > main_store_post = params[:record][:main_store].to_i || 0
> >
> > if !main_store_post.nil?&& main_store_post> 0
> > main_store = Store.find main_store_post
> > if !main_store.update_attributes :MainStore => 1
> > record.errors.add_to_base('The Main Store could not be set.')
> > else
> > # Create a new SyncConfig Store Record
> > scs = SyncConfigurationStore.create :store => main_store,
> >
> > :sync_configuration => record
> >
> > record.set_main_store_target = main_store
> > end
> > end
> > end
--
Sergio Cambra .:: entreCables S.L. ::.
Mariana Pineda 23, 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.