Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saving references to classes with attributes that have time datatypes #49

Open
ewamarciniak opened this issue Aug 9, 2014 · 6 comments

Comments

@ewamarciniak
Copy link

Hello there,

I have created a Project class and saved a couple of objects to the database. Those projects have attributes like delivery_deadline. Now created a class Document and instantiated it. I try to associate each document with a project and so trying to save it to the database. But this seems to fail as the reference is saved as json and seems like the simple quotes are the issue. This sql is generated by the ORM so is this a bug that I cannot save references of objects that have a time datatype attribute?
error_log

If I am doing something wrong here, please let me know. All the best.

@jgaskins
Copy link
Owner

jgaskins commented Aug 9, 2014

Can you show me your mappers either in this thread or in a gist?

@ewamarciniak
Copy link
Author

Here is a link to my app on git https://github.com/ewamarciniak/DM_app. I am trying to run the script called db_script.rb that is in the script folder to create some records in the database. The script is a bit messy, sorry about that but it is just work in progress. Thanks

@jgaskins
Copy link
Owner

jgaskins commented Aug 9, 2014

Embedding objects within other objects is a little wonky right now using perpetuity-postgres. This is a known bug, but I haven't really seen anyone use it so it's been low-priority. Specifically, the JSON serialization is a little weird. I need to take a closer look at that. Using a relational association with them works (just remove embedded: true), but you'll just have to use Mapper#load_association! to bring in the child object and it won't be loaded all within a single query.

@ewamarciniak
Copy link
Author

Thank you and yes that seems to work. I am trying to build 3 types of associations. I have built one app using AR as an ORM that includes 3 types of relationships and want to do the same here plus I want class TeamMember to inherit from the Person class. I used MTI in the other app. Is this possible using Perpetuity gem? Also in many to many relationship (Project vs Team Member) I would normally create another table that sits between those 2 classes and holds the ids. Since we are using references of the entire objects, how can this be done using Perpetuity? Or would you say there is another way. Any examples of this?
By the way I need to say that you have done a tremendous amount of work so far. Thumbs up for this :)

@ewamarciniak
Copy link
Author

Just to let you know the date error as described in this ticket is no longer an issue after I removed "embedded: true" but I would still appreciate if you could give me some pointers on how inheritance and many to many relationships can be build here. Thanks :)

@jgaskins
Copy link
Owner

Sorry for the delayed response. It's been a hectic week. :-)

For relationships between objects, here's what you can do currently (I use the concept of an article because it uses so many data relational concepts):

Perpetuity.generate_mapper_for Article do
  collection :articles # Not 100% necessary, but the default would be "Article", not "articles"

  attribute :title, type: String
  attribute :body, type: String
  attribute :author, type: Author
  attribute :comments, type: Array[Comment] # 1:N
end

If you look at the data in the articles table after saving them to the database, you'll see that the author and comments columns will have JSON data in them. The author will contain a single JSON object representing a reference to the persisted value that was in the object before it was saved. The comments column will have a JSON array of references to persisted comment objects.

This is not a traditional relational way to store relationships between objects, but it's very flexible on what you can store in the array. Using foreign keys as back references (like ActiveRecord's belongs_to) is probably a bit faster and easier to query, but requires everything to be stored in the same table, which means they all have to be of the same ActiveRecord::Base subclass or one of its descendants. Unfortunately, it's not yet supported in Perpetuity. It will likely have a different attribute syntax, though.

Here is how you would load referenced objects (taken from the README):

article_mapper = Perpetuity[Article]
articles = article_mapper.all.to_a
article_mapper.load_association! articles.first, :comments # 1:N
article_mapper.load_association! articles, :author         # All author objects for these articles load in a single query
article_mapper.load_association! articles, :comments       # M:N, loads all comments for all given articles

Storing those objects is as simple as inserting the top-level object. This will cascade through the entire tree and save all the objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants