Skip to content

Instantly share code, notes, and snippets.

@lolgolflol
Last active December 21, 2015 17:18
Show Gist options
  • Save lolgolflol/6339140 to your computer and use it in GitHub Desktop.
Save lolgolflol/6339140 to your computer and use it in GitHub Desktop.
I would like update all object that was child of User
class User
has_many :users_notes
has_many :notes, :through => :users_notes
end
class UsersNote
belongs_to :user
bolongs_to :notes
end
class Note
has_many :users_notes
has_many :users , :through => :users_notes
end
My example
user = User.new(:name=>"boo")
notes = []
notes << Note.new(:name=>"note1")
notes << note.new(:name => "note2")
user.notes = notes
user.save
/* result will two notes I suppose id of notes is 1,2 */
and then I will update User and Note as the same time.
user = User.find 1
user.name = "foo"
notes = []
note = Note.find 1
note.name = "noted 1"
notes << note
note2 = Note.find 2
note2.name = "noted 2"
notes << note2
user.notes = notes
user.save
but result wil not update 2 notes but user will be updated.
if will update 2 notes must remove relation table before for will update notes as same time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment