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

new vs build for associations #132

Closed
brendon opened this issue Nov 13, 2015 · 6 comments
Closed

new vs build for associations #132

brendon opened this issue Nov 13, 2015 · 6 comments

Comments

@brendon
Copy link

brendon commented Nov 13, 2015

I'm currently having a bit of a problem with a STI model.

I'm replacing existing working code to use decent_exposure and the only difference I can see in the code generated is that decent_exposure uses new instead of build to set up the new record from the parent association. I think this might be affecting how the STI code creates the object as now Rails is ignoring any validations on the STI's particular type model and is only running the validations on the parent model.

I was wondering what the reason was for using new instead of build in this case. Is it because build is sometimes unavailable?

I've really enjoyed implementing decent_exposure overall. It definitely clarifies the code and I can get rid of all those nasty before_actions!

@brendon
Copy link
Author

brendon commented Nov 13, 2015

Actually, it's related to assigning attributes separate from the initialisation of the object.

rails/rails@89b5b31

In Rails 4, the change above will initialise the object as its correct subclass based on the params[:model][:type] passed in, but it has to be done when the object is initialised.

I'll try to make a replacement strategy for this issue. Is it worth looking at making initialisation and attribute assignment in one step for decent_exposure in general?

@brendon
Copy link
Author

brendon commented Nov 14, 2015

I've had a go at this modification: #133

All tests pass (a couple had to be adjusted).

Let me know what you think.

@mattpolito
Copy link
Member

Closing post as v3.0 is a different codebase

@brendon
Copy link
Author

brendon commented Dec 4, 2017

I notice 3.0 uses new (not build) but at least passes in the params directly rather than in a two-step process. Apparently modern Rails doesn't have a difference between the two: https://stackoverflow.com/a/40989480/129798

@mattpolito
Copy link
Member

@brendon Are you having an issue then?

@brendon
Copy link
Author

brendon commented Dec 4, 2017

Sorry no, not that I can see. I was just posting that there in case anyone else found the issue.

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

No branches or pull requests

2 participants