The call to ActiveRecord #relationship_ids = [1,2,3] is immediately saved. Any workarounds?

I ran into unusual things in the ActiveRecord #relationship_ids method (which is automatically added when the has_many declaration), which is immediately saved for existing entries, which causes me some problems, and I am wondering if anyone has any useful tips.

I am running Rails 2.3.5.

Consider this simple scenario in which the article has_many tags, say:

a = Article.first
a.name = "New Name" # No save yet
a.author_id = 1     # No save yet
a.tag_ids = [1,2,3] # These changes are saved to the database
                    # immediately, even if I don't subsequently
                    # call 'a.save'

This seems surprising to me. This, in particular, causes problems when trying to create a preview tool - I want to update a bunch of attributes and then view the article without saving it, but in this case the tag changes are saved, although no other fields do.

(, "" - , , , , - , "a.save" )

- tag_ids = , before_save.

, has_many , .

- - / / ? !

+3
1

. . a , , , , .

, . , , , . save , .

. , , . Rails / , , .

, , , , , . accepts_nested_attributes_for, .

, , , . - act_as_audited, , .

, , , has_many, .

+3

Source: https://habr.com/ru/post/1732558/


All Articles