Git Product home page Git Product logo

Comments (5)

paulcsmith avatar paulcsmith commented on May 14, 2024

@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 build and not create, it does not get the id.

We could call insert when using params_for, but that could cause performance issues and bugs. Since we would insert the record and the association it would require more database inserts, and it would also insert the record. So if you're testing a create endpoint you would already have those records in the database, making it impossible (or very difficult) to test.

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. params_for_with_associations(:user)?

For now I think it would be best to write your own function like the one above, but I'm open to ideas!

from ex_machina.

geofflane avatar geofflane commented on May 14, 2024

@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?

from ex_machina.

paulcsmith avatar paulcsmith commented on May 14, 2024

@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 params_with_association_ids_for(:user), or full_params_for(:user) or something like that.

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?

from ex_machina.

geofflane avatar geofflane commented on May 14, 2024

@paulcsmith No apologies needed, I understood what you meant. :)

from ex_machina.

paulcsmith avatar paulcsmith commented on May 14, 2024

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

from ex_machina.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.