Mike, I guess what you want to do is something like this: http://pastie.org/598754
cheers snusnu On Sat, Aug 29, 2009 at 00:57, Mike Thompson<[email protected]> wrote: > > The basic goal here is that I have 2 categories of Url saved to a > campaign. They are primary and competitor. Both relationships are many > to many. The interaction I am hoping to model is one where basically a > boolean differentiator goes in the join table. Thus there is only one > table associating urls to the campaigns and it carries the > relationship of weather they are competitors or primary. Hopefully the > following snippets will more illustrate my goal. If someone could just > put me on the right path, that would be great. > > 1 class Campaign > 2 include DataMapper::Resource > 3 > 4 property :id, Serial > 5 property :name, String > 6 > 7 has n, :urls, :through => Resource > 8 > 9 end > > 1 class Url > 2 include DataMapper::Resource > 3 > 4 property :id, Serial > 5 property :url, String, :length => 255, :format => /(^$)|(^(http| > https):\/\/[a-z0-9\-\.]+([\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(([0-9]{1,5})? > \/.*)?$)/ix > 6 validates_is_unique :url > 7 > 8 > 9 end > > 1 class CampaignsUrl > 2 include DataMapper::Resource > 3 > 4 property :id, Serial > 5 property :url_id, Integer > 6 property :campaign_id, Integer > 7 property :competitor, Boolean > 8 > 9 > 10 has 1, :url > 11 has 1, :campaign > 12 > 13 end > > > And finally the spec file to illustrate: > > 1 require File.join( File.dirname(__FILE__), '..', "spec_helper" ) > 2 > 3 describe Campaign do > 4 > 5 it "should let me save a url as a primary and as a competitor > each shoudl create a unque row" do > 6 campaign = Campaign.new(:name => 'My first Campaign') > 7 campaign.save > 8 primary = Url.new(:url => 'http://www.google.com') > 9 primary.save > 10 competitor = Url.new(:url => 'http://www.bing.com') > 11 competitor.save > 12 campaign.primaries << primary > 13 campaign.competitors << competitor > 14 campaign.save > 15 camp_url = CampaignsUrl.first(:campaign_id => campaign.id, > url_id => primary.id) > 16 camp_url.competitor.should eql(false) > 17 camp_url = CampaignsUrl.first(:campaign_id => campaign.id, > url_id => competitor.id) > 18 camp_url.competitor.should eql(true) > 19 > 20 end > 21 > 22 end > > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "DataMapper" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/datamapper?hl=en -~----------~----~----~----~------~----~------~--~---
