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

Don't return id field in params_for #123

Merged
merged 1 commit into from
May 5, 2016
Merged

Don't return id field in params_for #123

merged 1 commit into from
May 5, 2016

Conversation

jsteiner
Copy link
Contributor

@jsteiner jsteiner commented May 5, 2016

I can't think of any use case where you would want the id field when
using params_for. For something scenarios like submitting to an API, or
getting the bare fields to test validations it would be rare to
actually submit the id itself, and so the current behavior feels
unexpected.

@paulcsmith
Copy link
Contributor

I like the idea of this, but I think we should delete the record's primary key instead of assuming it is :id.

You can get this with the functions that Ecto defines for schemas

MyModel.__schema__(:primary_key)

@paulcsmith
Copy link
Contributor

Also we should document that the primary key is removed


defp ecto_fields(struct) do
[:__meta__] ++ struct.__schema__(:associations) ++ struct.__schema__(:primary_key)
end
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this feels a bit roundabout because we already knowing it's dropping ecto fields because this function is called from drop_ecto_fields. So I think the abstraction is probably not necessary. What do you think about

record
|> Map.from_struct
|> Map.delete(:__meta__)
|> Map.delete(struct.schema__(:associations))
|> Map.delete(struct.schema__(:primary_key))

However, I don't feel strongly so if you do, feel free to leave it as is

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair.

@paulcsmith
Copy link
Contributor

Two small comments and then LGTM :D

I can't think of any use case where you would want the id field when
using params_for. For something scenarios like submitting to an API, or
getting the bare fields to test validations it would be rare to
actually submit the id itself, and so the current behavior feels
unexpected.
@jsteiner jsteiner merged commit dded301 into master May 5, 2016
@jsteiner jsteiner deleted the js-no-id branch May 5, 2016 21:03
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.

2 participants