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

is_changed on active_model #674

Closed
kudlatyamroth opened this issue Apr 14, 2022 · 1 comment · Fixed by #683
Closed

is_changed on active_model #674

kudlatyamroth opened this issue Apr 14, 2022 · 1 comment · Fixed by #683
Labels
good first issue Good for newcomers

Comments

@kudlatyamroth
Copy link

Summary

As of now, we can check if field of active model is changed, but we can't check if anything in whole active model has changed.
It would be great to just check if pear.is_changed() to make updated

Motivation

Now when i have active model and code like that:

async fn update_fruit(id: i64, color: Option<String>) -> Result<(), Error> {
    let apple: Option<fruit::Model> = Fruit::find_by_id(id).one(db).await?;
    let mut apple: Fruit::ActiveModel = apple.unwrap().into();

    if color.is_some() {
        apple.color = Set(color.unwrap());
    }

    let apple: fruit::Model = apple.update(db).await?;

    Ok(())
}

this code will produce error, cos it try to make Update query like that:
update fruit Set Where

With is_changed() method we could make:

if apple.is_changed() {
    apple.update(db).await?;
}

so, no errors will be produced

@billy1624
Copy link
Member

Hey @kudlatyamroth, yeah we can definitely add that. Actually, it's just one line of code loll

fruit::Column::iter().all(|col| active_model.get(col).is_changed())

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

Successfully merging a pull request may close this issue.

2 participants