1

so I'm new to join tables. I'm not sure if that's even what I need. This is my current setup and situation: I have 2 user models.

Company
User

I need to create a table that belongs to the company model, but can be answered by the User model. Each company can have several applications thus I called my 3rd model

Application 

so far I've done

class Application < ActiveRecord::Base
 belongs_to :company
end

and

Class Company < ActiveRecord::Base
  has_many :applications
end 

I created the application model straight forward by running:

rails g model application

have I already made some mistakes, or am I doing fine so far? and what are the next steps here.

1
  • It seems to be fine so far. Next step would be reading a nice tutorial to get to know the application flow you can try to go with guides.rubyonrails.org and railstutorial.org/book for the starters. Commented Jun 22, 2014 at 11:19

1 Answer 1

1

Seems good so far

--

Join Tables

There are two types of join table - has_and_belongs_to_many & has_many :through

enter image description here

They do the same thing (create a many-to-many relationship), which means you can call @model.objects through the join model

From reading your information, it looks like you could do this:

#app/models/user.rb
Class User < ActiveRecord::Base
   has_many :applications
   has_many :companies, through: :applications
end

#app/models/application.rb
Class Application < ActiveRecord::Base
   #fields - id | user_id | company_id | etc | etc | created_at | updated_at
   belongs_to :company
   belongs_to :user
end

#app/models/company.rb
Class Company < ActiveRecord::Base
   has_many :applications
   has_many :users, through: :applications
end
Sign up to request clarification or add additional context in comments.

1 Comment

I'm struggling with how to structure my database. So far I've come to add the company_id on the post. But my problem is this: A company has_many applications. They submit info like interview questions. Which will then be displayed in the show of that application, to users. The users will answer those questions and submit. So the applications belongs to the company, but needs to keep track of the ID of which user that answered the questions. Visible by the company

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.