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 a note about default_scope and create records #20763

Merged
merged 1 commit into from
Jul 18, 2015

Conversation

maurogeorge
Copy link
Contributor

I think that is not so clear when you change a default scope this changes the new and create of a Model, so I think worthwhile add a note about it on the default scope guide

@@ -1266,6 +1266,9 @@ class Client < ActiveRecord::Base
end
```

NOTE: The `default_scope` is also applied while creating/building a record.
It is not applied while updating a record.
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth adding an example to the note? Something like:

class User < ActiveRecord::Base
  default_scope { where active: true }
end

User.create! name: 'foo'
=> #<User id: 1, name: "foo", active: true>

User.unscoped.create! name: 'bar'
=> #<User id: 2, name: "bar", active: nil>

Otherwise, LGTM 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@repinel thanks for your feedback. I took a look in some notes in random sections of the guides and none of shows a example of code, I think the Idea is to be a small note only.

Copy link
Member

Choose a reason for hiding this comment

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

This one have an example.
NOTE: Using where like this will only work when you pass it a Hash. For SQL-fragments you need to use references to force joined tables:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right, I look for the code inside the note and not after 😑

Updated with example of code.

@sgrif
Copy link
Contributor

sgrif commented Jul 18, 2015

Seems reasonable. Thanks.

sgrif added a commit that referenced this pull request Jul 18, 2015
Add a note about default_scope and create records [ci skip]
@sgrif sgrif merged commit 2a0a264 into rails:master Jul 18, 2015
@maurogeorge
Copy link
Contributor Author

Thanks for the merge ❤️

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

Successfully merging this pull request may close these issues.

None yet

3 participants