-
-
Notifications
You must be signed in to change notification settings - Fork 725
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 query from variant_overrides_controller to its model scope #2818
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,6 @@ class VariantOverride < ActiveRecord::Base | |
|
||
acts_as_taggable | ||
|
||
attr_accessor :import_date | ||
|
||
belongs_to :hub, class_name: 'Enterprise' | ||
belongs_to :variant, class_name: 'Spree::Variant' | ||
|
||
|
@@ -21,6 +19,12 @@ class VariantOverride < ActiveRecord::Base | |
where(hub_id: hubs) | ||
} | ||
|
||
scope :distinct_import_dates, lambda { | ||
select('DISTINCT variant_overrides.import_date'). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A pity that we're still on Rails 3.2 otherwise we could make use of AR's distinct |
||
where('variant_overrides.import_date IS NOT NULL'). | ||
order('import_date DESC') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just as a minor detail, could we rely on ActiveRecord's hash style? So it'd be like See the examples in https://apidock.com/rails/ActiveRecord/QueryMethods/order. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apparently this also works only after Rails 4 :/ as well as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really? 😭 |
||
} | ||
|
||
localize_number :price | ||
|
||
def self.indexed(hub) | ||
|
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.
This was overriding the ActiveRecord getter/setter, which is why querying didn't work for me. When defining new accessor methods, shouldn't they have a different name than the database field?
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.
Great question, I got the opportunity to improve my activerecord basics...
Because import_date is a database column in the variant_override table, this line is useless and I can be deleted because AR already does this for each db table field.
@Matt-Yorkley will confirm if there's any magical stuff going on here.
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.
Did we mean attr_accessible instead, perhaps 🤔 ?
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 dont think so, attr_accessible is for mass-assignment. attr_accessor is what you get out of the box in active records. AR adds the getter and setter (like attr_accessor) for each db column.
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.
Looks fine to delete 👍