Hi Kristian,

The code I have shown is not the exact code I am using - it has to be
saved to the database. I just gave this code so it was easier to
follow.

> without looking into the datamapper code, but the create method is on
> the model class and NOT on instance. my guts feeling is to do
> something like
>
> after :save do
> self.url ='/mysite/' << self.id.to_s
> self.save!
> end

How come self.save!, what's the difference with this and self.save?

cheers,

DAZ



On Apr 6, 3:00 pm, kristian <meier.krist...@gmail.com> wrote:
> even in case I am on the wrong track, but there is no need to have the
> url in the database, just have a method
>
> def url
>   "/mysite/#{self.id}"
> end
>
> in case you want to be able set the url with something differently
> after create, you could do something like
>
> def url
>    u = attribute_get(:url)
>    if u
>      u
>    else
>      "/mysite/#{self.id}"
>    end
> end
>
> so basically a database NULL will be your default otherwise return the
> database value
>
> without looking into the datamapper code, but the create method is on
> the model class and NOT on instance. my guts feeling is to do
> something like
>
> after :save do
> self.url ='/mysite/' << self.id.to_s
> self.save!
> end
>
> regards, Kristian
>
> On Apr 6, 11:02 am, DAZ <daz4...@gmail.com> wrote:
>
>
>
>
>
>
>
> > 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