I have two simple models each with acts_as_tree, say Departments and Employees.
My goal is to create a treeview combining both models in to one overall tree, like so:
Department 1
SubDepartment 1.1
Employee A
Employee B
SubDepartment 1.2
Department 2
Subdepartment 2.1
Employee C
Department 3
SubDepartment 3.1
Employee D
Employee E
Subdepartment 3.2
etc
I found this already: Acts as Tree with Multiple Models but I'm afraid I could use a little more pointers in the right direction.
Thanks!
So your schema is like this?
Department
acts_as_tree #requires departments.parent_id field
has_many :employees
Employee
belongs_to :department #requires employees.department_id field
I would just stick with this rather than trying to make the tree 'know' about employees. The only things that have the tree relationship are the departments. The employees belong to a department but they're not part of the tree structure.
As far as editing goes, then, when you change a department you set parent_id to be the id of its parent in the tree, and when you move an employee you set department_id to be the id of its 'parent'.
What's your actual problem? I mean what are you trying to do?
Related
I have 2 models: nation (table nations) and majors (table majors).
nations contains all the nations of the world and majors contains all the educational branches like Computer Science, Mechanical Engineering etc.
Both models have id and name fields and contain the following relationships.
Nation model (Nation.rb)
has_many :majors
Major model (Major.rb)
has_many :nations
I want to run a query like: Find all the majors where nation_id = x (where x is the id of nation).
How do I do this in rails?
I feel there should exist a table which contains a mapping like:
id major_id nation_id
1 1 1
2 1 2
3 2 1
.
.
.
where each major is mapped to the country to which it belongs and vice versa.
I am new to rails and not sure how to do this.
You're right about creating a third table for the mapping between the two. You have two options. You can either create a third table in case you want fields in it other than major_id and nation_id.
If not, you should go with creation of the join table. Feel free to try it and let me know if their are other issues.This article explains it in a very simple way.
I have 4 Models, each defined as follows
--Model/Table--
Competition
Matches
Goals
Teams
These 4 models completes a season's worth of soccer league data. The relationship between these tables are already defined. Now, I want to create a class where it picks up data from all 4 models and generate 1, new class. Like so :
class CompetitionSeason < ApplicationRecord
# Use data from all 4 models to construct a soccer season class
end
That is my intention, but I'm not sure if this is the best practice for doing so. It feels... wrong on a Rails framework level. What is the best practice to achieve this?
Thank you.
You should really have another model called Season or some such which owns (has_(one|many)) each of the constituents.
I'm not sure what Competition is.
Match can belong to either a Competition or directly to Season. I don't get what Competition is so I can't make a good recommendation.
Goal should belong to Match or Competition, and not directly to Season. Season can still have many goals through Match or Competition.
Team should have a SeasonTeam junction table.
I've had some issues with this before when creating applications and I think I'm starting to run into it again, hence I'm asking this on StackOverflow to save me a lot of time.
I've spent the last few weeks setting up a perfected product model for my system. The model performs exactly as I want it to and has several complex features (such as search via sunspot). I wanted to setup the category to product structure before I started this heavy development - however struggling with this kind of thing was just putting me off creating the application so I got straight into the product structure.
Now I've got the product model setup - what would be the easiest way to add a category ownership to encompass the products? (All products have a category_id column which store their father category id)
My plan is to have the category index to be a list of all the categories, the category show to be a list of the products inside that category and the product show being the view of the actual product. This would eliminate the product index and so I'll have to come up with a way to port the search feature (sunspot) from my index view to the category show somehow.
As for the actual listing of the products - I assume I'll have to do some kind of partial? (I don't know a lot about it).
Most basically, my relationships are planned to be:
category:
has_many :products
product:
has_one :category
My products then have a category_id column to store the ID of it's parent category.
Any tips on how to accomplish the relationships (category show to list the products etc)?
Best Regards,
Joe
Relationships like the one you're wanting are built into ActiveRecord support. Understanding the model relationships in Rails is critical to doing anything in Rails that's non-trivial, so study up.
Also, the relationship you're looking for is something like:
product:
belongs_to :category
category:
has_many :products
I am am developing a recruitment application for storing records of people who come for an interview. So I have two models - Applicant & Employee which seem like one in OO sense. i.e. Employee was earlier an applicant. Therefore I am planning to create a Single table inheritance like:
Applicant < Person
Employee < Person
All the fields of Applicant are there in Employee. Employee has several other fields that are not in Applicant.
Am I correct with this plan. I have another similar scenario to deal with - prospects & clients.
Update: As I am working more and more with STI, I am starting to dislike it.
As per my personal opinion , in OO point of view you are correct.
Applicant < Person
Employee < Person
But when it comes to database/ tables i will go for a one table called 'persons' (Person) that has all the columns for both 'Applicant' and 'Employee' and have a column with a flag indicating whether the Person is an Applicant or Employee
NOTE :: but this is only true if you have ONLY 'several other fields' not many
HTH
sameera
In my application there are courses which have steps. (A user proceeds though the course by viewing the steps in order)
But there are many types of steps (Quiz, Text, Video..etc). In this example I will show 2 of the steps.
Here is my database/model design so far:
steps
id
step_type_id
client_id
title
summary
position
Relationships:
belongs_to: client
belongs_to: step_type
has_one: step_quiz
has_one step_text
step_quizzes
id
step_id
instructions
correct_to_pass
retakes_allowed
time_limit
Relationships:
belongs_to: step
has_many: quiz_questions
step_texts
id
step_id
content
Relationships:
belongs_to: step
It was suggested to me before that this is a polymorphic relationship, but I guess I don't see how it is. I see it as an inheritance relationship where a sub step type inherits its basic information from the steps table. Can this be modeled differently?
Seems that in this example the best approach would be to use STI. Polymorphic associations are better for the inverse relationship - eg. when there would be multiple things that could have the same step (courses, workshops,...).