Skip to content
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

Add has_many :miq_requests in ServiceTemplate #17242

Merged
merged 1 commit into from
Apr 5, 2018

Conversation

bzwei
Copy link
Contributor

@bzwei bzwei commented Apr 2, 2018

Add has_many :miq_requests association in ServiceTemplate.
It helps to find out all the miq_requests made from the service_template

@bzwei bzwei changed the title Add association to miq_requests Add has_many :miq_requests in ServiceTemplate Apr 2, 2018
@bzwei
Copy link
Contributor Author

bzwei commented Apr 2, 2018

@miq-bot add_label transformation, enhancement
@miq-bot assign @gmcculloug

@@ -55,6 +55,8 @@ class ServiceTemplate < ApplicationRecord

has_many :dialogs, -> { distinct }, :through => :resource_actions

has_many :miq_requests, ->(template) { where(["source_id = ? AND source_type = 'ServiceTemplate'", template.id]).order(:created_on) }, :foreign_key => "source_id"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is .order(:created_on) really required here? I feel like this should be left to the caller to append if needed.

Also, does this need to be accessible from the API? cc @abellotti

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove the sorting. Was following examples from other model.
Checked with @abellotti. No changed is needed from API side.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this as simple as has_many :miq_requests, :as => :source?
http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this feels like revinventing Rails.

@gmcculloug
Copy link
Member

@bzwei Can you take a look at the two rubocop issues.

@kbrock
Copy link
Member

kbrock commented Apr 4, 2018

Can you put in 1 test to make sure the 2 models link together properly.

Rubocop is complaining about:

  • :dependent says want to do to the remote fields if this record is deleted.
  • :inverse_of says the inverse of this relationship. Which seems odd since they are not compatible with :polymorphic => true or :as associations. Hence why I suggest a trivial test.

I'm thinking you want:

has_many :miq_requests, :as => :source, :dependent => :nullify

maybe throw in :inverse_of => "source" for kicks

@gmcculloug
Copy link
Member

@kbrock Have the robot overloads officially taken over? Should we really be adding :inverse_of based on rubocop feedback when the Active Record doc says:

There are a few limitations to :inverse_of support:

They do not work with :through associations.
They do not work with :polymorphic associations.
They do not work with :as associations.

From: http://guides.rubyonrails.org/association_basics.html

@gmcculloug
Copy link
Member

@bzwei I am in favor of dropping :inverse_of from the association since it appears to be useless and adding it here is confusing.

As for tests, I would not expect you to test this association alone, but where this is used. What is the intended purpose of this change and can those changes be included in this PR and tested?

@bzwei
Copy link
Contributor Author

bzwei commented Apr 5, 2018

The purpose of the PR is for API to be able to include miq_requests as an attribute.

@miq-bot
Copy link
Member

miq-bot commented Apr 5, 2018

Checked commit bzwei@2bd37e2 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
1 file checked, 1 offense detected

app/models/service_template.rb

Copy link
Member

@bdunne bdunne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Yeah, we generally avoid testing Rails things (like relations) since they're usually tested through many other paths.

@bdunne bdunne merged commit b452c96 into ManageIQ:master Apr 5, 2018
@bdunne bdunne assigned bdunne and unassigned gmcculloug Apr 5, 2018
@bdunne bdunne added this to the Sprint 83 Ending Apr 9, 2018 milestone Apr 5, 2018
agrare pushed a commit to agrare/manageiq that referenced this pull request May 25, 2018
Add has_many :miq_requests in ServiceTemplate

(cherry picked from commit b452c96)
@himdel
Copy link
Contributor

himdel commented May 29, 2018

@miq-bot add_label gaprindashvili/yes
@miq-bot remove_label gaprindashvili/no

Otherwise getting "Invalid attributes specified: miq_requests", when trying to retrieve a list of migration plans in v2v. (ManageIQ/manageiq-ui-classic#3963)

(the request: /api/service_templates/?filter[]=type=%27ServiceTemplateTransformationPlan%27&expand=resources&attributes=name,description,miq_requests,options,created_at&sort_by=updated_at&sort_order=desc)

simaishi pushed a commit that referenced this pull request May 29, 2018
Add has_many :miq_requests in ServiceTemplate
(cherry picked from commit b452c96)
@simaishi
Copy link
Contributor

Gaprindashvili backport details:

$ git log -1
commit 5b62d0168b9118aae4680c6ee5eae2f299aba5c5
Author: Brandon Dunne <brandondunne@hotmail.com>
Date:   Thu Apr 5 12:02:12 2018 -0400

    Merge pull request #17242 from bzwei/template_and_request
    
    Add has_many :miq_requests in ServiceTemplate
    (cherry picked from commit b452c96cdfdc4bda0ffc711c2c8296fd85bb094d)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants