# Question:How do I interpret the relationship between the schema.rb file and the models?

by bsugar |

I'm trying to create a database schema diagram (a.k.a entity relationship diagram) of the PLOTS database using the data model in conjunction with the schema.rb file. I found a program online called dbdiagram.io program which allows you to upload schema.rb files to create these diagrams, and also export them as .sql files (which is nice if you'd prefer to use MySQL Workbench).

There are three things that I am seeking more information about regarding the PLOTS schema.rb and models file:

1) While there are answer models, question controllers, and question views, am I correct in understanding that a "question" is actually just a research note with a power tag?

2a) Take, for example, the comment model. Does the image below reflect the accompanying code?

2b) How does the last line has_many :likes, :as => :likeable fit into the picture? There are functions in the model for liked_by, likers, emoji_likes, and user_reactions_map but how are the likes connected to the comments model?



class Comment < ApplicationRecord

include CommentsShared # common methods for comment-like models

belongs_to :node, foreign_key: 'nid', touch: true, counter_cache: true

# dependent: :destroy, counter_cache: true

belongs_to :user, foreign_key: 'uid'

has_many :likes, :as => :likeable



Hi @bsugar!

1) While there are answer models, question controllers, and question views, am I correct in understanding that a "question" is actually just a research note with a power tag?

That's right!

2a) Take, for example, the comment model. Does the image below reflect the accompanying code?

I think so... it looks right!

2b) How does the last line has_many :likes, :as => :likeable fit into the picture? There are functions in the model for liked_by, likers, emoji_likes, and user_reactions_map but how are the likes connected to the comments model?

I believe this is a complex case because one can like a node, a comment, or (maybe? I forget) an answer. Also, reacting with an emoji is a form of liking - or, you could say, when you star something in the UI, you are liking it in one way, but if you react with an emoji you're doing something similar but with a different kind of like.

From a quick skim of this search: https://github.com/publiclab/plots2/search?q=likeable

I believe likeable is a generic term for something that can be liked, including comments and nodes. I hope that helps!

As to the broader Q: How do I interpret the relationship between the schema.rb file and the models?

The schema.rb should describe the database itself. In fact it's possible to run something like rake db:dump (not sure exact command) to generate a schema.rb file from a live database, with no information at all from the Rails app itself. This "teaches" the rails app what's in the database and what structure it has. Then we build models over that which extend and inter-relate the database tables with relationships (has_many, belongs_to) and methods. For more on this, see https://guides.rubyonrails.org/active_record_basics.html

This roughly follows this pattern: https://en.wikipedia.org/wiki/Active_record_pattern

Hope this helps!

Is this a question? Click here to post it to the Questions page.

Yes! Perfect. Thank you! Speaking of a node, this is sort of the "base content" which can either be a wiki page or a research note, and a research note can become a question with the power tag? Users, comments, answers, and tags have their own (and sometimes inter-related) tables?

Is this a question? Click here to post it to the Questions page.

that's right!

Very likable indeed. Thanks, @warren!