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

Model.new returning a pre-initialised object in class method #35666

Open
sonal425chouhan opened this issue Mar 19, 2019 · 4 comments
Open

Model.new returning a pre-initialised object in class method #35666

sonal425chouhan opened this issue Mar 19, 2019 · 4 comments

Comments

@sonal425chouhan
Copy link

Steps to reproduce

  1. I have a Model named Project and a class method in project.rb called 'set_new_project'.

def self.set_new_project
Project.new
end

  1. run the 'where' query on Project i.e a = Project.where(id: 1)
  2. Call a.set_new_project

Expected behaviour

This should return an uninitialised object i.e
<Project id: nil, name: nil, description: nil>

Actual behaviour

It returns an id initialised object i.e
<Project id: 1, name: nil, description: nil>

Additional Information:

  • if you run Project.new.reload in set_new_project then it will return an object with all fields initialised as that of 'a'
  • if you run Project.all then it will return a

System configuration

Rails version:
Rails 5.2.0.rc1
Ruby version:
2.4.2

@rafaelfranca
Copy link
Member

Could you create a reproduction script with this template https://github.com/rails/rails/blob/master/guides/bug_report_templates/active_record_master.rb?

@rafaelfranca rafaelfranca added activerecord more-information-needed When reporter needs to provide more information labels Mar 19, 2019
@andrehjr
Copy link
Contributor

Went ahead and created a template for you @sonal425chouhan https://gist.github.com/andrehjr/efda765d33883489d01ab603f7613047#file-issue35666-rb

What's happening is that when you call a 'class' method on a ActiveRecord::Relation. It creates a 'scope'. When you call Project.new, it gets created using the scope of that relation.

See:
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/scoping.rb#L35-L45
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/delegation.rb#L102

A "quick fix" would be to calling Project.unscoped.new, which I'm not sure is the best solution.

@rails-bot rails-bot bot closed this as completed Apr 2, 2019
@rails-bot
Copy link

rails-bot bot commented Apr 2, 2019

This issue has been automatically closed because there has been no follow-up response from the original author. We currently don't have enough information in order to take action. Please reach out if you have any additional information that will help us move this issue forward.

@rafaelfranca rafaelfranca reopened this Apr 2, 2019
@rafaelfranca rafaelfranca added With reproduction steps and removed more-information-needed When reporter needs to provide more information labels Apr 2, 2019
@st0012
Copy link
Contributor

st0012 commented Apr 5, 2019

IMO, initializing objects like this with scope attributes sounds legit, but the scope attributes shouldn't include primary key

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

Successfully merging a pull request may close this issue.

4 participants