Thanks for pointing out my schoolboy error there Jon!

This now works:
post '/new' do
  @page = Page.create(params[:page])
    @page.url = '/mysite/' << @page.id.to_s
    @page.save
 ....etc

But this feels really ugly - surely this logic should be in the model,
not the controller? In this implementation, the url only gets saved
when the resource is created on the web.

Is there a way to do something similar using the after :create hook? I
can't see why this doesn't work, as it should do the same as the code
above:

 after :create do
    self.url ='/mysite/' << self.id.to_s
    self.save
 end


cheers,

DAZ




On Apr 5, 10:59 pm, Jonathan Stott <jonathan.st...@gmail.com> wrote:
> For that to work, you want `...@page.id` not `self.id`
>
> Regards
> Jon
>
> On 5 April 2011 22:55, DAZ <daz4...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Since I couldn't get the after :create hook to work, I tried putting
> > this ugly code in a handler:
>
> > post '/new' do
> >  @page = Page.create(params[:page])
> >    @page.url = '/mysite/' << self.id.to_s
> >    @page.save
>
> > This 'works', but strangely the id seems to be an object id, rather
> > than the id property from the database. What's going on here??
>
> > There must be a way of using a resource's id property to set another
> > property ... surely?
>
> > cheers,
>
> > DAZ
>
> > On Apr 5, 5:47 pm, DAZ <daz4...@gmail.com> wrote:
> >> > Is there any 'best practice' way of doing this sort of thing?
>
> >> > For example, do I use an after :create hook and then call self.save at
> >> > the end?
> >> > eg
> >> >  after :create do
> >> >     self.url ='/mysite/' << self.id.to_s
> >> >     self.save
> >> >  end
>
> >> Well the 'best practice' way is definitely not this way because it
> >> returns false - the resource is created but then is not saved
> >> afterwards and the url property is considered 'dirty' (what does that
> >> actually mean??
>
> >> I'm puzzled by this!
>
> >> DAZ
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "DataMapper" group.
> > To post to this group, send email to datamapper@googlegroups.com.
> > To unsubscribe from this group, send email to 
> > datamapper+unsubscr...@googlegroups.com.
> > For more options, visit this group 
> > athttp://groups.google.com/group/datamapper?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to datamapper@googlegroups.com.
To unsubscribe from this group, send email to 
datamapper+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to