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
fields_for/params_for and belongs_to associations #104
Comments
@geofflane This is a good point. This is actually expected behavior. Since Ecto doesn't have an id until the record is inserted we can't set the id. Since params_for calls We could call I do see your point though. We have code in one of our apps that does something like this def play_params do
game = create(:game)
posession_team = create(:posession_team)
params_for(:play, game_id: game.id, posession_team: posession_team.id)
end Maybe it would be useful to have an option for creating associations when using params_for, but I'm not sure how that would look, or if it would be more trouble than it's worth. For now I think it would be best to write your own function like the one above, but I'm open to ideas! |
@paulcsmith I mainly put this issue in because it was unexpected behavior to me. The create "does the right thing" and builds the graph of objects in the DB, but the build/params only kind of does. (That params_for model is exactly what I came up with too. I found that pain because I added a constraint that caused a bunch of my tests to fail so I had to go add those associations manually in a bunch of places.) The use case, for me, is testing an API POST to create a new instance of a thing. I wanted a way to easily construct an instance, but also to have dependent relations exist already. There might be a hybrid in there somewhere between create and build/params_for maybe to support that case? |
@geofflane Sorry I probably should have phrased that better. What I meant by "expected behavior" was that it is not a bug, but I can definitely see how it is unexpected. One thing I would recommend is to add the params function to your factory or in a module that you import in your tests, that way when you need to change how it works you only need to do it in one place. As for the params_for function, I doubt that we'll change it to insert associations by default because that could cause unintended side effects. I'm not opposed to adding another function like What do you think @jsteiner. Also @derekprior, we had to create a custom function for handling relationships on our project. What do you think of having a function that inserts associations and sets their ids? |
@paulcsmith No apologies needed, I understood what you meant. :) |
Thanks @geofflane! I created another issue #105 for implementing this. Feel free to tackle it if you have time :) If not I can get it in a few weeks |
fields_for (now params_for) doesn't populate the IDs of relations when you build them.
Imagine you're modeling a play in a sport...
With
When you do
fields_for(:play)
game_id and possession_team_id are nil. It makes it harder to use for sending data to an API, for example. It would be nice if those could be populated.Technically when you do
build(:play)
the same is true. It seems like Ecto doesn't populate those IDs until after it's saved?The text was updated successfully, but these errors were encountered: