Converting a rails application to lift

3 views
Skip to first unread message

Achint Sandhu

unread,
Mar 2, 2010, 2:56:38 PM3/2/10
to Lift
Hi,

I'm new to scala (2.7.7) and lift (2.0-M2) and as a learning exercise
have taken on the translation of an existing rails project into a lift
application.

There are two things I have run into that I'm hoping the more
experienced members of the list can give me a hand with:

1) Is there a trait in lift that creates and manages an equivalent of
the createdAt and updatedAt fields that rails provides? I'm thinking
something along the lines of IdPK, but have been unable to find
anything.

2) I've been following the wiki article on setting up One-to-Many
relationships (http://wiki.github.com/dpp/liftweb/how-to-work-with-one-
to-many-relationships) and am running into a difference in behaviour.
Following the example, if I look at anAuthor.books, I get back a List
of Book objects, however when I look at aBook.author, I get back a
Long with the ID of the Author. I would expect aBook.author to return
an Author object. I've copied and pasted the example in the wiki, to
make sure that it wasn't my implementation.

Other than that, so far, it's gone extremely well and I was able to
get something up and running very quickly which really is a testament
to the design of the framework.

Thanks.

Cheers,
Achint

David Pollak

unread,
Mar 2, 2010, 3:21:07 PM3/2/10
to lif...@googlegroups.com
On Tue, Mar 2, 2010 at 11:56 AM, Achint Sandhu <achint...@gmail.com> wrote:
Hi,

       I'm new to scala (2.7.7) and lift (2.0-M2) and as a learning exercise
have taken on the translation of an existing rails project into a lift
application.

       There are two things I have run into that I'm hoping the more
experienced members of the list can give me a hand with:

1) Is there a trait in lift that creates and manages an equivalent of
the createdAt and updatedAt fields that rails provides? I'm thinking
something along the lines of IdPK, but have been unable to find
anything.


There's nothing right now.  Feel encouraged to open a ticket at https://liftweb.assembla.com/spaces/liftweb/tickets for a feature request.
 
2) I've been following the wiki article on setting up One-to-Many
relationships (http://wiki.github.com/dpp/liftweb/how-to-work-with-one-
to-many-relationships
) and am running into a difference in behaviour.
Following the example, if I look at anAuthor.books, I get back a List
of Book objects, however when I look at aBook.author, I get back a
Long with the ID of the Author. I would expect aBook.author to return
an Author object. I've copied and pasted the example in the wiki, to
make sure that it wasn't my implementation.
 

       Other than that, so far, it's gone extremely well and I was able to
get something up and running very quickly which really is a testament
to the design of the framework.

       Thanks.

Cheers,
Achint

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




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

Mads Hartmann Jensen

unread,
Mar 2, 2010, 3:26:17 PM3/2/10
to lif...@googlegroups.com

On 02/03/2010, at 20.56, Achint Sandhu wrote:

> Hi,
>
> I'm new to scala (2.7.7) and lift (2.0-M2) and as a learning exercise
> have taken on the translation of an existing rails project into a lift
> application.
>
> There are two things I have run into that I'm hoping the more
> experienced members of the list can give me a hand with:
>
> 1) Is there a trait in lift that creates and manages an equivalent of
> the createdAt and updatedAt fields that rails provides? I'm thinking
> something along the lines of IdPK, but have been unable to find
> anything.
>
> 2) I've been following the wiki article on setting up One-to-Many
> relationships (http://wiki.github.com/dpp/liftweb/how-to-work-with-one-
> to-many-relationships) and am running into a difference in behaviour.
> Following the example, if I look at anAuthor.books, I get back a List
> of Book objects, however when I look at aBook.author, I get back a
> Long with the ID of the Author. I would expect aBook.author to return
> an Author object. I've copied and pasted the example in the wiki, to
> make sure that it wasn't my implementation.
>

You should be able to get the object by calling aBook.author.obj - this should return a Box[Author] so you could get it like this

aBook.author.obj match {
case Full(a) => a // do something with the autor
case Empty => // if the box is empty, handle it somehow
case _ => // should cover everyhting else, Failure etc
}

Hope it helps


> Other than that, so far, it's gone extremely well and I was able to
> get something up and running very quickly which really is a testament
> to the design of the framework.
>
> Thanks.
>
> Cheers,
> Achint
>

Naftoli Gugenheim

unread,
Mar 2, 2010, 3:36:58 PM3/2/10
to lif...@googlegroups.com
You can assign the ticket to me because I have code for such fields that I can contribute.

-------------------------------------
David Pollak<feeder.of...@gmail.com> wrote:

On Tue, Mar 2, 2010 at 11:56 AM, Achint Sandhu <achint...@gmail.com>wrote:

> Hi,
>
> I'm new to scala (2.7.7) and lift (2.0-M2) and as a learning
> exercise
> have taken on the translation of an existing rails project into a lift
> application.
>
> There are two things I have run into that I'm hoping the more
> experienced members of the list can give me a hand with:
>
> 1) Is there a trait in lift that creates and manages an equivalent of
> the createdAt and updatedAt fields that rails provides? I'm thinking
> something along the lines of IdPK, but have been unable to find
> anything.
>
>

There's nothing right now. Feel encouraged to open a ticket at
https://liftweb.assembla.com/spaces/liftweb/tickets for a feature request.

> 2) I've been following the wiki article on setting up One-to-Many
> relationships (http://wiki.github.com/dpp/liftweb/how-to-work-with-one-

> to-many-relationships<http://wiki.github.com/dpp/liftweb/how-to-work-with-one-%0Ato-many-relationships>)


> and am running into a difference in behaviour.
> Following the example, if I look at anAuthor.books, I get back a List
> of Book objects, however when I look at aBook.author, I get back a
> Long with the ID of the Author. I would expect aBook.author to return
> an Author object. I've copied and pasted the example in the wiki, to
> make sure that it wasn't my implementation.
>


>


> Other than that, so far, it's gone extremely well and I was able to
> get something up and running very quickly which really is a testament
> to the design of the framework.
>
> Thanks.
>
> Cheers,
> Achint
>
> --
> You received this message because you are subscribed to the Google Groups
> "Lift" group.
> To post to this group, send email to lif...@googlegroups.com.
> To unsubscribe from this group, send email to

> liftweb+u...@googlegroups.com<liftweb%2Bunsu...@googlegroups.com>


> .
> For more options, visit this group at
> http://groups.google.com/group/liftweb?hl=en.
>
>

--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Surf the harmonics

--

Achint Sandhu

unread,
Mar 2, 2010, 4:15:21 PM3/2/10
to Lift
Just a quick note to thank everyone for the prompt responses.

Thank You.

Cheers,
Achint

On Mar 2, 3:36 pm, Naftoli Gugenheim <naftoli...@gmail.com> wrote:
> You can assign the ticket to me because I have code for such fields that I can contribute.
>
> -------------------------------------
>

> David Pollak<feeder.of.the.be...@gmail.com> wrote:


>
> On Tue, Mar 2, 2010 at 11:56 AM, Achint Sandhu <achint.san...@gmail.com>wrote:
>
>
>
>
>
> > Hi,
>
> >        I'm new to scala (2.7.7) and lift (2.0-M2) and as a learning
> > exercise
> > have taken on the translation of an existing rails project into a lift
> > application.
>
> >        There are two things I have run into that I'm hoping the more
> > experienced members of the list can give me a hand with:
>
> > 1) Is there a trait in lift that creates and manages an equivalent of
> > the createdAt and updatedAt fields that rails provides? I'm thinking
> > something along the lines of IdPK, but have been unable to find
> > anything.
>

> There's nothing right now.  Feel encouraged to open a ticket athttps://liftweb.assembla.com/spaces/liftweb/ticketsfor a feature request.


>
>
>
>
>
> > 2) I've been following the wiki article on setting up One-to-Many
> > relationships (http://wiki.github.com/dpp/liftweb/how-to-work-with-one-

> > to-many-relationships<http://wiki.github.com/dpp/liftweb/how-to-work-with-one-%0Ato-many-re...>)


> > and am running into a difference in behaviour.
> > Following the example, if I look at anAuthor.books, I get back a List
> > of Book objects, however when I look at aBook.author, I get back a
> > Long with the ID of the Author. I would expect aBook.author to return
> > an Author object. I've copied and pasted the example in the wiki, to
> > make sure that it wasn't my implementation.
>
> >        Other than that, so far, it's gone extremely well and I was able to
> > get something up and running very quickly which really is a testament
> > to the design of the framework.
>
> >        Thanks.
>
> > Cheers,
> > Achint
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Lift" group.
> > To post to this group, send email to lif...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > liftweb+u...@googlegroups.com<liftweb%2Bunsu...@googlegroups.com >
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/liftweb?hl=en.
>
> --
> Lift, the simply functional web frameworkhttp://liftweb.net

> Beginning Scalahttp://www.apress.com/book/view/1430219890

Achint Sandhu

unread,
Mar 2, 2010, 4:01:49 PM3/2/10
to Lift
Ticket created -
https://liftweb.assembla.com/spaces/liftweb/tickets/390-request-for-trait-that-supports-createdat-and-updatedat

Thank You.

Cheers,
Achint

On Mar 2, 3:36 pm, Naftoli Gugenheim <naftoli...@gmail.com> wrote:

> You can assign the ticket to me because I have code for such fields that I can contribute.
>
> -------------------------------------
>

> David Pollak<feeder.of.the.be...@gmail.com> wrote:


>
> On Tue, Mar 2, 2010 at 11:56 AM, Achint Sandhu <achint.san...@gmail.com>wrote:
>
>
>
>
>
> > Hi,
>
> >        I'm new to scala (2.7.7) and lift (2.0-M2) and as a learning
> > exercise
> > have taken on the translation of an existing rails project into a lift
> > application.
>
> >        There are two things I have run into that I'm hoping the more
> > experienced members of the list can give me a hand with:
>
> > 1) Is there a trait in lift that creates and manages an equivalent of
> > the createdAt and updatedAt fields that rails provides? I'm thinking
> > something along the lines of IdPK, but have been unable to find
> > anything.
>

> There's nothing right now.  Feel encouraged to open a ticket athttps://liftweb.assembla.com/spaces/liftweb/ticketsfor a feature request.


>
>
>
>
>
> > 2) I've been following the wiki article on setting up One-to-Many
> > relationships (http://wiki.github.com/dpp/liftweb/how-to-work-with-one-

> > to-many-relationships<http://wiki.github.com/dpp/liftweb/how-to-work-with-one-%0Ato-many-re...>)


> > and am running into a difference in behaviour.
> > Following the example, if I look at anAuthor.books, I get back a List
> > of Book objects, however when I look at aBook.author, I get back a
> > Long with the ID of the Author. I would expect aBook.author to return
> > an Author object. I've copied and pasted the example in the wiki, to
> > make sure that it wasn't my implementation.
>
> >        Other than that, so far, it's gone extremely well and I was able to
> > get something up and running very quickly which really is a testament
> > to the design of the framework.
>
> >        Thanks.
>
> > Cheers,
> > Achint
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Lift" group.
> > To post to this group, send email to lif...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > liftweb+u...@googlegroups.com<liftweb%2Bunsu...@googlegroups.com >
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/liftweb?hl=en.
>
> --
> Lift, the simply functional web frameworkhttp://liftweb.net

> Beginning Scalahttp://www.apress.com/book/view/1430219890

Achint Sandhu

unread,
Mar 2, 2010, 8:09:55 PM3/2/10
to Lift
Hi,

Is there any reason why aBook.author would not simply return a Box
instead of requiring aBook.author.obj to get the Box ?

I'm sure there is a really good reason for this and I'm just trying to
get an understanding of the underlying reasoning.

Thanks.

Cheers,
Achint

Naftoli Gugenheim

unread,
Mar 2, 2010, 8:19:34 PM3/2/10
to lif...@googlegroups.com
Yes, because, as in the database, essentially it's a MappedLong (for example), with support built on top of it (via trait mixins) to lookup and cache the referenced entity.
Is there a problem with the .obj syntax?

-------------------------------------

David Pollak

unread,
Mar 2, 2010, 8:21:38 PM3/2/10
to lif...@googlegroups.com
On Tue, Mar 2, 2010 at 5:09 PM, Achint Sandhu <achint...@gmail.com> wrote:
Hi,

Is there any reason why aBook.author would not simply return a Box
instead of requiring aBook.author.obj to get the Box ?



In Lift's Mapper fields are not flat collections of bytes like in ActiveRecord, but entities that can control access, format, etc. their values.

aBook.author is the field.  The field is a MappedLongForeignKey.  It has lots and lots of methods on it to get, set, validate, generate forms, etc.

To make things a little more convenient, the fields know how to convert themselves to their "raw byte" values.  So a MappedLongForeignKey is a MappedField[Long] and knows how to convert itself into a Long.  For example:

val x: Long = aBook.author // legal

But MappedLongForeignKey fields need to be explicitly converted into the object that they are keys to via the .obj method:

val author: Box[Author] = aBook.author.obj

I guess it would be nice to write a convenience conversion to make:

val author: Box[Author] = aBook.author 

legal.  Feel free to open a ticket on this.

 
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.




--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890

Jonathan Hoffman

unread,
Mar 2, 2010, 8:32:10 PM3/2/10
to lif...@googlegroups.com
I'm sure you've got this covered, but I've also had this requirement and used something like this: http://gist.github.com/320200

Achint Sandhu

unread,
Mar 3, 2010, 8:16:49 AM3/3/10
to Lift
Thank your the code snippet. It's a lot cleaner than what I have.

I'll wait for the official implementation so as not to get into an
IP / copyright issues.

Thanks again for posting.

Cheers,
Achint

On Mar 2, 8:32 pm, Jonathan Hoffman <jonhoff...@gmail.com> wrote:
> I'm sure you've got this covered, but I've also had this requirement and used something like this:http://gist.github.com/320200
>
> On Mar 2, 2010, at 4:01 PM, Achint Sandhu wrote:
>
>
>
> > Ticket created -

> >https://liftweb.assembla.com/spaces/liftweb/tickets/390-request-for-t...


>
> > Thank You.
>
> > Cheers,
> > Achint
>
> > On Mar 2, 3:36 pm, Naftoli Gugenheim <naftoli...@gmail.com> wrote:
> >> You can assign the ticket to me because I have code for such fields that I can contribute.
>
> >> -------------------------------------
>
> >> David Pollak<feeder.of.the.be...@gmail.com> wrote:
>
> >> On Tue, Mar 2, 2010 at 11:56 AM, Achint Sandhu <achint.san...@gmail.com>wrote:
>
> >>> Hi,
>
> >>>        I'm new to scala (2.7.7) and lift (2.0-M2) and as a learning
> >>> exercise
> >>> have taken on the translation of an existing rails project into a lift
> >>> application.
>
> >>>        There are two things I have run into that I'm hoping the more
> >>> experienced members of the list can give me a hand with:
>
> >>> 1) Is there a trait in lift that creates and manages an equivalent of
> >>> the createdAt and updatedAt fields that rails provides? I'm thinking
> >>> something along the lines of IdPK, but have been unable to find
> >>> anything.
>

> >> There's nothing right now.  Feel encouraged to open a ticket athttps://liftweb.assembla.com/spaces/liftweb/ticketsfora feature request.

Achint Sandhu

unread,
Mar 3, 2010, 8:25:10 AM3/3/10
to Lift
There is no problem with the .obj syntax, just that I found it a
little un-natural given how it works in the other direction
(anAuthor.books). I've raised a ticket as per David's request.

On Mar 2, 8:19 pm, Naftoli Gugenheim <naftoli...@gmail.com> wrote:
> Yes, because, as in the database, essentially it's a MappedLong (for example), with support built on top of it (via trait mixins) to lookup and cache the referenced entity.
> Is there a problem with the .obj syntax?
>
> -------------------------------------
>

Achint Sandhu

unread,
Mar 3, 2010, 8:26:44 AM3/3/10
to Lift
Thank you for the explanation David.

I've raised a ticket as requested:
http://www.assembla.com/spaces/liftweb/tickets/394-request-for-convenience-conversion-on-mappedlongforeignkey-fields

Cheers,
Achint

On Mar 2, 8:21 pm, David Pollak <feeder.of.the.be...@gmail.com> wrote:


> On Tue, Mar 2, 2010 at 5:09 PM, Achint Sandhu <achint.san...@gmail.com>wrote:
>
> > Hi,
>
> > Is there any reason why aBook.author would not simply return a Box
> > instead of requiring aBook.author.obj to get the Box ?
>

> For some background, seehttp://blog.lostlake.org/index.php?/archives/19-Keeping-the-meaning-w...

> > liftweb+u...@googlegroups.com<liftweb%2Bunsu...@googlegroups.com >


> > .
> > > > For more options, visit this group athttp://
> > groups.google.com/group/liftweb?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Lift" group.
> > To post to this group, send email to lif...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > liftweb+u...@googlegroups.com<liftweb%2Bunsu...@googlegroups.com >


> > .
> > For more options, visit this group at
> >http://groups.google.com/group/liftweb?hl=en.
>
> --
> Lift, the simply functional web frameworkhttp://liftweb.net

> Beginning Scalahttp://www.apress.com/book/view/1430219890

Naftoli Gugenheim

unread,
Mar 3, 2010, 3:27:55 PM3/3/10
to lif...@googlegroups.com
Is a syntax like this okay (inside an Author mapper)?
object created extends MappedDateTime(this) with CreatedTimestamp[Author]
Although if we say that timestamps are always a date-time (not date only or time only) this could be shortened to
object created extends CreatedTimestamp(this)


-------------------------------------
Achint Sandhu<achint...@gmail.com> wrote:

Hi,

I'm new to scala (2.7.7) and lift (2.0-M2) and as a learning exercise
have taken on the translation of an existing rails project into a lift
application.

There are two things I have run into that I'm hoping the more
experienced members of the list can give me a hand with:

1) Is there a trait in lift that creates and manages an equivalent of
the createdAt and updatedAt fields that rails provides? I'm thinking
something along the lines of IdPK, but have been unable to find
anything.

2) I've been following the wiki article on setting up One-to-Many

relationships (http://wiki.github.com/dpp/liftweb/how-to-work-with-one-


to-many-relationships) and am running into a difference in behaviour.
Following the example, if I look at anAuthor.books, I get back a List
of Book objects, however when I look at aBook.author, I get back a
Long with the ID of the Author. I would expect aBook.author to return
an Author object. I've copied and pasted the example in the wiki, to
make sure that it wasn't my implementation.

Other than that, so far, it's gone extremely well and I was able to


get something up and running very quickly which really is a testament
to the design of the framework.

Thanks.

Cheers,
Achint

--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.

To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.

Achint Sandhu

unread,
Mar 4, 2010, 4:37:33 AM3/4/10
to Lift
Hi,

I'll prefix this by saying that I'm new to this and not qualified to
comment, but I would expect the timestamp to always be DateTime.

That having been said, either syntax is fine.

Cheers,
Achint


On Mar 3, 3:27 pm, Naftoli Gugenheim <naftoli...@gmail.com> wrote:
> Is a syntax like this okay (inside an Author mapper)?
> object created extends MappedDateTime(this) with CreatedTimestamp[Author]
> Although if we say that timestamps are always a date-time (not date only or time only) this could be shortened to
> object created extends CreatedTimestamp(this)
>
> -------------------------------------
>

Achint Sandhu

unread,
Mar 6, 2010, 2:14:35 PM3/6/10
to Lift
Hi,

I've run into a slight snag with my use of the current approach. I
suspect what I'm trying to do is silly. The interaction below is from
the lift console and I don't actually use open_! in my code.

---
scala> val b = Book.find(1).open_!
l: com......Book = com......Book={id=1,author=1}

scala> b.author
res3: b.author.type = 1

scala> b.author == 1
res4: Boolean = true
---

If for some reason a Book does not have an author (possible in my
applications case), b.author would return NULL and comparing b.author
== 1 returns Boolean = false

This approach works in the actual application, but the compiler
generates the following warning.

---
warning: comparing values of types Long and object b.author using `=='
will always yield false
---

In the specific application case I have, I have access to the
Author.id and want to check if the book is referring to the author
whose ID I have before I do something. I know that I could look up the
Author object and do the comparison, but the above seemed to be a
useful shortcut.

Is there a recommended (succinct) way of accomplishing the above ?

I'd like the thank everyone for the patience and help provided so far.

Cheers,
Achint

On Mar 2, 3:26 pm, Mads Hartmann Jensen <mads...@gmail.com> wrote:

Achint Sandhu

unread,
Mar 6, 2010, 2:23:10 PM3/6/10
to Lift
Hi,

Small update.

Changing the order of the comparison in my code

i.e. b.author == 1 instead of 1 == b.author

fixed the warning, but I'm still curious as to whether what I'm doing
is a good idea.

Thanks.

Cheers,
Achint

Reply all
Reply to author
Forward
0 new messages