Of course, one option would be to do: class Interest include DataMapper::Resource
# properties property :user_id, Integer, :key => true property :question_id, Integer, :key => true timestamps :at # associations belongs_to :user, :child_key => [:user_id] belongs_to :question, :child_key => [:question_id] def save question = self.question question.interested_users = question.interested_users+1 self.question.save super end end No special magic required! On Mon, Mar 9, 2009 at 2:06 PM, Rafael George <george.raf...@gmail.com>wrote: > > Thanks it's look like that the way to go, but i tried it and did not > work. Here is the code for the two model involve and the code for the > hook. > > class Question > > include DataMapper::Resource > > # properties > > property :id, Serial > property :title, String, :nullable => false, :length => (1..255) > property :body, Text > property :interested_users, Integer, :default => 0 > > timestamps :at > > # associations > > belongs_to :user > > has n, :answers > has n, :interests > has n, :users, :through => :interests > > end > > class Interest > > include DataMapper::Resource > > # properties > property :user_id, Integer, :key => true > property :question_id, Integer, :key => true > timestamps :at > > # associations > > belongs_to :user, :child_key => [:user_id] > belongs_to :question, :child_key => [:question_id] > > # hooks > before :save, :save_interested > > def save_interested > question = self.question > question.interested_users = question.interested_users+1 > self.question.save > end > end > > > On Mon, Mar 9, 2009 at 12:03 PM, Rafael <rafaelros...@gmail.com> wrote: > > > > Hi, > > > > I'm not a PHP developper, and I'm quite new to DataMapper, but I'm > > pretty sure this is not the right approach. I believe you should use > > the hooks (aka callbacks or events) to make this kind of process. The > > reference is here http://datamapper.org/doku.php?id=docs:hooks, and > > your code should look like this: > > > > class Interest > > include DataMapper:Resource > > # properties > > property :user_id, Integer, :key => true > > property :question_id, Integer, :key => true > > property :interested_users, Integer, :default => 0 > > timestamps :at > > > > # associations > > belongs_to :user, :child_key => [:user_id] > > belongs_to :question, :child_key => [:question_id] > > > > # hooks > > before :save, :add_one > > > > protected > > def add_one > > self.interested_users = self.interested_users + 1 > > end > > end > > > > I believe the hooks are called inside a transaction, so it should be > > safe to do this. I used "interested_users" as Integer just to > > illustrate, but you can fix it according to your needs. > > > > Unfortunately the documentation is very scarce, I hope to have some > > time to help fix that in the near future. Hope this helps. > > > > Cheers, > > Rafael > > www.rafaelrosafu.com > > > > > > > > > -- > Rafael George > > > > -- Yehuda Katz Developer | Engine Yard (ph) 718.877.1325 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "DataMapper" group. To post to this group, send email to datamapper@googlegroups.com To unsubscribe from this group, send email to datamapper+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/datamapper?hl=en -~----------~----~----~----~------~----~------~--~---