Skip to content

Instantly share code, notes, and snippets.

@jacobsmith
Last active January 2, 2016 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobsmith/8225462 to your computer and use it in GitHub Desktop.
Save jacobsmith/8225462 to your computer and use it in GitHub Desktop.
I'm working on a Rails app that's designed for research projects. I have what I think the model/database layout should be,
but am having trouble implementing it (thus, I figured there was probably something small, either in implementation or
design, that should be looked at). The overall schema I'm working with is as follows:
User
| -username
| -password
|
\
Project
| -name
|
\
Source
| -title
| -author
| -URL
| -comments
\
Note
-quote
-comments
The way I have it set up currently is as follows:
User has_many :projects
Projects belongs_to :user
Projects has_many :sources
Sources belongs_to :projects
Sources has_many :notes
Notes belongs_to :sources
The problem I am running into is that I want each of these to be descendants of its parents (Thus, a user will create an
account, create a project, and then everything after that is a descendant of just that project. The user can also create
different projects, each of which are a self-contained "eco-system" if you will).
My current thought is to save :project_id to a cookie and then access it as a hidden value in the source submit form (save
parent_id, include it with the create child request). Or, abstract that away into :current_project, :current_source
methods through helpers like in Devise :current_user.
This seems a bit clunky so I was just wondering if you had any suggestions!
Thanks
-Smitty
@swanson
Copy link

swanson commented Jan 2, 2014

I think you typically want to avoid hidden fields/cookies if possible. I like to think of it by the URL:

To create a source for a project, I will probably be on a page like

/projects/:project_id/sources/new

Submitted a form would hit the SourcesController#create method, where you can set the project_id using the URL parameters (params[:project_id]).

You only need to go up one "level" in the hierarchy - when you create a Note you just need to get it attached to the right Source; the Source already has the link to the Project; the Project is already linked to a user.

Something else you should look into is has_many :foo, through: :bar. You can use this to walk down the hierarchy: http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

That would allow you to easily get, e.g. all the notes for a given project. It also allows you to say given a note, what Project is it attached too.

This is kind of a tricky ActiveRecord concept - so play around with it. I still have to look back at the docs to figure out how it all works from time to time.

Disclaimer: I haven't done Rails stuff in a few months, so might be some small inaccuracies :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment