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

update_attributes without :tag_list key sets tag_list to [] #547

Closed
snitko opened this issue May 26, 2014 · 8 comments
Closed

update_attributes without :tag_list key sets tag_list to [] #547

snitko opened this issue May 26, 2014 · 8 comments

Comments

@snitko
Copy link

snitko commented May 26, 2014

I have a problem while updating the model that is taggable. If I call update_attributes, but without the :tag_list - then all tags are removed from this object. Example:

q = Question.create(title: "How do I create tags?", tag_list: "ruby, rails, programming")
q.tag_list #=> ["ruby", "rails", "programming"]
q.update_attributes(title: "How do I delete tags?")
q.tag_list #=> []

So wrong. If I don't touch :tag_list, it shouldn't delete all of the existing tags on this model.

@seuros
Copy link
Collaborator

seuros commented May 26, 2014

What version are you using ? I can't replicate this with the 3.2.5!

@snitko
Copy link
Author

snitko commented May 26, 2014

3.2.3
I'll try 3.2.5 and will report. Thank you.

@seuros seuros closed this as completed in 66d8ef2 May 26, 2014
@seuros
Copy link
Collaborator

seuros commented May 26, 2014

I added a test for it. Just to be sure that it won't break later.

@snitko
Copy link
Author

snitko commented May 27, 2014

Ok, I figured it out. The problem was validation. I had a validation that checked the number of tags in the tag_list. It didn't allow to save the model unless there were between 3 and 15 tags in this list.

Now, the problem is, that in between the time #update_attributes is called and before the model is actually saved, tag_list stays empty. Therefore, I can't really validate it properly because although the tag_list will eventually not change, at the time of validation it is empty and the validation fails.

In your test that you've added in response to my issue, you can clearly see that problem: you have to #reload the model. Obviously, this is impractical during the validation. Would you recommend any solution to that?

@seuros
Copy link
Collaborator

seuros commented May 27, 2014

I added reload just to be sure that i'm testing the database data and not the in memory one.
The tests will pass even without the reload.

Can you write a test case ?

@snitko
Copy link
Author

snitko commented May 27, 2014

Ok, I wrote a test to specifically address the validation issue and it passed: snitko@2d1f238

Which makes me think there is indeed something wrong with my app. If you don't mind, I'll report back if I manage to write a test that fails. And anyway, thank you for making me do it and discovering the problem further.

@seuros
Copy link
Collaborator

seuros commented May 27, 2014

Ne za chto.
Please let me updated.

@aldosolis
Copy link

aldosolis commented Oct 26, 2016

Any solution to this yet? I got the same problem in the following context:

class User 
  belongs_to :login 
  ...
end
class Login
   # tags
    acts_as_ordered_taggable_on :udfs
end
user = User.find(1)
user.update_attributes(login_attributes: { id: user.login.id, udf_list: "a, b, c", first_name: "Aldo" })

problem: model.login.name is updated but model.logins.udf_list is not updated

Using version 4.0.0

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

3 participants