Hi,
I'm have this two classes
class User
include DataMapper::Resource
property :id, Serial
property :name, String
has n :posts, :through => Resource
end
class Post
include DataMapper::Resource
property :id, Serial
property :title, String
property :body, Text
has n :users, :through => Resource
end
So once I have a new post like:
Post.new(:title => "Hello World", :body = "Hi there").save
I'm having serious problems to add and remove from the association, like:
User.first.posts << Post.first #why do I have to save this as oppose from AR?
(User.first.posts << Post.first).save #this just works if saving the insertion later
and how should I remove a post from that association? I'm using the following but definitely its not working:
User.first.posts.delete(Post.first) #returns the Post.first, but nothing happens
User.first.posts.delete(Post.first).save #returns true, but nothing happpens
User.first.posts.delete(Post.first).destroy #destroy the Post.first, not the association
So I really don't know how to delete this from the BoltUser Array,
Any Help please?
Thank you very much :)
UPDATE: I Managed to do it by doing:
#to add
user_posts = User.first.posts
user_posts << Bolt.first
user_posts.save
#to remove
user_posts.delete(Bolt.first)
user_posts.save
I think the only way to do it is working with the instance actions, do your changes on that instance and after you finished, just save it. It's kind of different from AR but, its cool though.
I'm not sure if its possible to do it with any other method, but this is fine for now :)