-
-
Notifications
You must be signed in to change notification settings - Fork 498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move Transfer.storage_locations_transferred_to/from_in to StorageLoca… #4733
base: main
Are you sure you want to change the base?
Move Transfer.storage_locations_transferred_to/from_in to StorageLoca… #4733
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the move. One comment about the implementation.
app/models/storage_location.rb
Outdated
@@ -77,6 +77,14 @@ def self.items_inventoried(organization, inventory = nil) | |||
end | |||
end | |||
|
|||
def self.with_transfers_to(organization) | |||
joins(:transfers_to).where(organization_id: organization.id).distinct.order(:name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't need the where
clause since the transfer has a from_id
and to_id
which powers the association.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Storage locations belong to an organization and so do transfers. So if you have a transfer, by definition only storage locations belonging to the same organization can be involved in it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I still don't understand why this where
clause is unnecessary. We are only joining the transfers
and storage_locations
tables on the storage_locations
primary key and the transfers
foreign keys from_id
and to_id
. So it will return records for different organizations unless we specify an organization_id
somewhere. (Or like if we moved the method to be an instance method on Organization
so the id is specified by default.)
To illustrate in rails console in development:
-
Organization 1 has a transfer from storage location 1 to storage location 2. Organization 2 has a transfer from storage location 3 to storage location 4.
-
If I remove the
where
clause, then I get all the storage locations that have been transferred to (locations 2 and 4), which correspond to different organizations.
-
When I add back in the
where
clause, I see only storage locations that apply to a certain organization which I thought is what we wanted.
Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, I was missing the fact that this was a class-level method instead of an instance-level method.
I think this could be clearer if you made these scopes instead of class-level methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok changed them to be scopes instead 👍
app/models/storage_location.rb
Outdated
@@ -77,6 +77,14 @@ def self.items_inventoried(organization, inventory = nil) | |||
end | |||
end | |||
|
|||
def self.with_transfers_to(organization) | |||
joins(:transfers_to).where(organization_id: organization.id).distinct.order(:name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, I was missing the fact that this was a class-level method instead of an instance-level method.
I think this could be clearer if you made these scopes instead of class-level methods.
(Doesn't resolve any issue)
Description
While working on something else, I noticed this
Transfer
class method that seemed odd in that it returns an array ofStorageLocation
ActiveRecord objects. Feels like it belongs more on theStorageLocation
model. Moving it also simplified the method logic.Also fixed the association's
foreign_key
so that ActiveRecord can join the table correctly.Type of change
Refactor
How Has This Been Tested?
Moved model spec from
Transfer
toStorageLocation
.