This project is archived and is in readonly mode.

#619 ✓ resolved
David Dollar

Allow :accessible => true associations to update records

Reported by David Dollar | July 14th, 2008 @ 05:10 PM | in 2.x

Adds the ability to update existing entries on :accessible => true associations


# create from a basic hash
> p = Post.create(:title => 'Test Post', :author => { :name => 'David' })
=> #<Post id: 8, author_id: 8, title: "Test Post", created_at: "2008-07-14 15:22:53", updated_at: "2008-07-14 15:22:53">

# update a singular reference
> p.author = { :name => 'Joe' }
=> {:name=>"Joe"}

# it 'updates' the row in sql, notice the id is still the same
> p.author
=> #<Author id: 8, name: "Joe", created_at: "2008-07-14 15:22:53", updated_at: "2008-07-14 15:23:03">


# create an author with posts from a hash
>  a = Author.create(:name => 'David', :posts => [ { :title => 'Post 1' }, { :title => 'Post 2' } ])
=> #<Author id: 14, name: "David", created_at: "2008-07-14 15:38:18", updated_at: "2008-07-14 15:38:18">

# show the posts
> a.posts
=> [#<Post id: 17, author_id: 14, title: "Post 1", created_at: "2008-07-14 15:38:18", updated_at: "2008-07-14 15:38:18">, #<Post id: 18, author_id: 14, title: "Post 2", created_at: "2008-07-14 15:38:18", updated_at: "2008-07-14 15:38:18">]

# use << to update existing entries (as well as add new ones, demonstrated later)
> a.posts << { :id => 17, :title => 'Post 1 Updated' }
=> [#<Post id: 17, author_id: 14, title: "Post 1 Updated", created_at: "2008-07-14 15:38:18", updated_at: "2008-07-14 15:38:53">, #<Post id: 18, author_id: 14, title: "Post 2", created_at: "2008-07-14 15:38:18", updated_at: "2008-07-14 15:38:18">]

# show posts to verify the update
> a.posts
=> [#<Post id: 17, author_id: 14, title: "Post 1 Updated", created_at: "2008-07-14 15:38:18", updated_at: "2008-07-14 15:38:53">, #<Post id: 18, author_id: 14, title: "Post 2", created_at: "2008-07-14 15:38:18", updated_at: "2008-07-14 15:38:18">]

# can't update posts that don't belong to the author
> a.posts << { :id => 1, :title => 'Not Allowed' }
ActiveRecord::RecordNotFound: Couldn't find Post with ID=1 AND ("posts".author_id = 14)
        from /Users/ddollar/Code/EdgeRailsApp/vendor/rails/activerecord/lib/active_record/base.rb:1393:in `find_one'
        from /Users/ddollar/Code/EdgeRailsApp/vendor/rails/activerecord/lib/active_record/base.rb:1376:in `find_from_ids'
        from /Users/ddollar/Code/EdgeRailsApp/vendor/rails/activerecord/lib/active_record/base.rb:537:in `find'
        from /Users/ddollar/Code/EdgeRailsApp/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:47:in `find'
        from /Users/ddollar/Code/EdgeRailsApp/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:103:in `<<'
        from /Users/ddollar/Code/EdgeRailsApp/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:99:in `each'
        from /Users/ddollar/Code/EdgeRailsApp/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:99:in `<<'
        from /Users/ddollar/Code/EdgeRailsApp/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `transaction'
        from /Users/ddollar/Code/EdgeRailsApp/vendor/rails/activerecord/lib/active_record/transactions.rb:79:in `transaction'
        from /Users/ddollar/Code/EdgeRailsApp/vendor/rails/activerecord/lib/active_record/transactions.rb:98:in `transaction'
        from /Users/ddollar/Code/EdgeRailsApp/vendor/rails/activerecord/lib/active_record/associations/association_collection.rb:98:in `<<'
        from (irb):12
        
# use = to outright replace all posts
> a.posts = [ { :title => 'Replace Posts' } ]
=> [#<Post id: 19, author_id: 14, title: "Replace Posts", created_at: "2008-07-14 15:40:30", updated_at: "2008-07-14 15:40:30">]

# can even 'replace' using existing posts, the post attributes will be updated
> a.posts = [ { :id => 19, :title => 'Can Replace This Way Too' } ]
=> [#<Post id: 19, author_id: 14, title: "Can Replace This Way Too", created_at: "2008-07-14 15:40:30", updated_at: "2008-07-14 15:40:49">]

# use << also for adding brand new items
> a.posts << { :title => 'New Post' }
=> [#<Post id: 19, author_id: 14, title: "Replace Posts", created_at: "2008-07-14

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Tickets have moved to Github

The new ticket tracker is available at https://github.com/rails/rails/issues

Shared Ticket Bins

Attachments

Pages