Two Objects, Yet So Many Relationships
Tags: rails, ruby
Published: 2021-04-30 17:12:18

Rails was by far the most challenging part of the Flatiron Course for me so far, but after completing my project my understanding has grown vastly. Ruby on Rails allows us to create multidimensional relationships between two objects in a really easy way. The use of partials help render the proper forms to submit data based on whether a relationships currently exist. In my case, I delved into two different relationships between my User Object and my Jobs Object. 
##Models
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/egun6ev74hpkra7fclfg.png)
 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mtvr7l8nutvv22qto2c4.png)
## User_Job 
The first relationship to explain here is the has_many through user_jobs relationship. The User can have many jobs through user jobs and vice versa. There are also boolean attributes that can indicate whether a User is, in this case, interested or has "applied" to this job. * Also to be clear this would have been easier to understand in hindsight if I called this relationship something like interests.
##Schema of Related Tables
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vg5kmzakle31x6ldfq1e.png)
##Rendering With Partials
This relationship is used to create an interest field at the top of the Job objects show field. This is done through partials rendered depending on this following code ``` <% @user_job =  @user_jobs.find_by(job_id: @job.id, user_id: current_user.id)%>
<%= render :partial => "/user_jobs/edit", locals: {user_job: @user_job} %>
<% else %>
<% @user_job = UserJob.new %>
<%= render :partial => "/user_jobs/new", locals: {user_job: @user_job} %>
<% end %>```
Based on this function the application will render a partial to either create a new relationship between User and Job or edit an existing relationship. 
 ![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qt6qb6z10gd6rqt09z9w.png)

Once that form is submitted the User_Job_Controller determines whether the params passed through are valid to update the database. The create and update are almost identical so I only show create here.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bh0e1h0gjqcyeogcb3tj.png)


##Creating Reviews: Need For Relationship Independence
After getting this set up though, reviews was the next step and I did not want anytime a User says their interested/applying to a job that this would inherently establish a review as well. When it comes down to it on any job posting site, reviews left for a job should function independently of a User being interested in a job.
 
##Reviews Model
This is where I had to build in a reviews model. A User and a Job can have many reviews but a review can only belong to one User and one Job. This relationship is a little more simple than the previous model. Here a User can have many reviews, a Job can have many reviews, but a Review belongs to a job and a user.
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/riucrl4s3hkvt1sahwsm.png)
 With that established now all that was needed was to make the resources to be able to CRUD these reviews. On top of that a User should be able to leave a review at the bottom of a Job show page. Utilizing a nested new form I am able to give the user the ability to submit that nested review. The nested form utilizes this function below that basically says "Hey! If there is an instance of job associated with the new review being written on this page, do not allow the user to select another job to associate this review with!". As you can see below if there is no associated job than a dropdown will allow you to choose. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6nx8bc6ioc0e0gajlwy1.png)
Below that nested form on the Job show page all past reviews for this job are shown. This was accomplished through an each loop and displays the review title, content, and author. 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0bpfds4kezode1hjm3g.png)
 
This rails section has blown me away with what I am now able to achieve and the most daunting topic, OOP, is becoming more and more clear. These Show pages could be cleaned up with more partials and I'm going to be implementing those in short time. All in all though I am impressed with what I can now comprehend after only four weeks of exposure to this framework and I cannot wait to build further projects that are even more expansive and intuitive.