Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Patrick Montalto
Patrick Montalto
7,868 Points

ActiveRecord through association

I'm having some difficulty understanding the through: association.

I'm creating a model called Experiments, which has a Foreign Key to Formula. Formulas also have many Formula Line Items. Experiments also have many Experiment Line Items.

When the user selects the Formula for the Experiment, I want the collection set of Formula Line Items available for those Experiment Line Items to be limited to the Formula Line Items which belong to the selected Formula.

For example, if Formula A has 4 line items and Formula A was chosen for the Experiment, I would want the Experiment Line Items to only be limited to those 4 Formula Line Items.

Also, the same Formula can appear in multiple Experiments, therefore the Formula Line Items can be referenced by multiple sets of Experiment Line Items.

It is my understanding that I will have to use a through: association to achieve this. But I don't quite understand the relationship in ActiveRecord. Do I need to create another model to serve as the join model or do one of the aforementioned models serve as the join model?

4 Answers

Seth Kroger
Seth Kroger
56,403 Points

So the question is, do you want the Experiment Line Items updated if there are any changes to the Formula Line Item (which is what would happen with has_many :through) or are you keeping a log of the experiment as it was done and not change them if the procedure for the Formula changes in the future? If it's the second it may be better to copy the Line Items in the ExperimentsController create() method instead of using an association.

Patrick Montalto
Patrick Montalto
7,868 Points

I have clarified that the Experiment Line Items should not be updated if changes are made to the Formula Line Items. They are locked at creation. I will instead copy the line items in the ExperimentsController create() method as you mentioned. Thanks!

Seth Kroger
Seth Kroger
56,403 Points

From what I can see you don't need a 3rd model as a go-between. It does need to be used in conjunction with has_many.

Class Experiment < ActiveRecord::Base
  has_one :formula
  has_many :formula_line_items, through: :formula
end

It doesn't look like you can directly name the column experiment_line_items but you can alias it with alias_attribute.

Patrick Montalto
Patrick Montalto
7,868 Points

I have an experiment_line_items table as well as a formula_line_items table. The model for Experiment you provided would then associate the formula_line_items with the Experiment through the selected Formula foreign key, but what about the experiment_line_items ? Maybe I am not clear on what I thought I could achieve with it. I see each experiment_line_item belonging_to an experiment and also belonging_to a formulation_line_item through the formula selected to the given experiment. Would this make sense? It's kind of a double through relationship. Knowing this, would the given model you provided have to be modified or would it work ?

class ExperimentLineItem < ActiveRecord::Base
  belongs_to :experiment
  belongs_to :formula_line_items, through: experiment, through: formula
end

or could the second through: be omitted if the Experiment model you provided is used?

Kevin Korte
Kevin Korte
28,109 Points

So I want to make sure I understand this correct first.

Experiments has many experiment line items Formulas has many formula line items

Experiments has many forumulas Formulas belong to experiements

That's the overall relationships?

For clarification, what are the experiment and formula line items? Are these truly different line items? Or can these be combined, because it sounds like each has it's own model but do they need to? Can a line item belong to both experiments and formulas.

Patrick Montalto
Patrick Montalto
7,868 Points

Replied below. I feel like there is definitely an easier way to achieve what I am picturing.

Patrick Montalto
Patrick Montalto
7,868 Points

A Formula has many Formula Line Items.

A Formula has many Experiments.

An Experiment belongs to a Formula. (It's data is based off the Formula it references).

An Experiment has many Experiment Line Items (each Experiment Line Item references the associated Formula Line Item(s)).

For example:

Formula Table
| id | job_request | 
|----|-------------|
|  1 |     522     | 
|  2 |     523     | 

Formula Line Item table 
| id | dna | concentration | formula_id |
|----|-----|---------------|------------|
|  1 | C1  |      30       |     1      |
|  2 | C2  |      50       |     1      |
|  3 | C2  |      90       |     2      |


Experiment table 
| id |     title    |  formula_id |
|----|--------------|-------------|
|  5 | Experiment 5 |      1      |
|  6 | Experiment 6 |      1      |

Experiment Line Items table 
| id | experiment_id |  formula_line_item_id | diluted_volume |
|----|---------------|-----------------------|----------------|
|  1 |       5       |            1          |       50       |
|  2 |       5       |            2          |       60       |
|  3 |       6       |            1          |       90       |
|  4 |       6       |            2          |      100       |

As you can see, each Formula Line Item is used as a kind of template for the Experiment Line Items. The Formula serves as sort of a recipe and the Experiment serves as a record of actually carrying out that recipe.