Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6133327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:12:49+00:00 2026-05-23T17:12:49+00:00

I am working with DataMapper and Sinatra to create a simple app. Here’s the

  • 0

I am working with DataMapper and Sinatra to create a simple app. Here’s the structure:

The app has Accounts. Each account has users and campaigns. Each user has comments that should be related to a specific campaign.

Comments should ideally have a user_id and a campaign_id to relate them both.

How can I relate the 2 together? Here’s the code that I have which does not work:

class Account
  include DataMapper::Resource
  property :id, Serial
  property :mc_username, String, :required => true
  property :mc_user_id, String, :required => true
  property :mc_api_key, String, :required => true
  property :created_at, DateTime
  property :updated_at, DateTime
  has n, :users
  has n, :campaigns
end

class User
  include DataMapper::Resource
  property :id, Serial
  property :name, String, :required => true
  property :email, String, :required => true
  property :is_organizer, Integer
  property :created_at, DateTime
  property :updated_at, DateTime
  belongs_to :account, :key => true
  has n, :comments
end

class Campaign
  include DataMapper::Resource
  belongs_to :mailchimpaccount, :key => true
  has n, :comments
  property :id, Serial
  property :cid, String
  property :name, String
  property :current_revision, Integer
  property :share_url, Text, :required => true
  property :password, String
  property :created_at, DateTime
  property :updated_at, DateTime
end

class Comment
  include DataMapper::Resource
  belongs_to :campaign, :key => true
  belongs_to :user, :key => true
  property :id, Serial
  property :at_revision, Integer
  property :content, Text
  property :created_at, DateTime
end

With this code, I can’t save a comment since I can’t figure out how to associate it to a campaign and a user at the same time. I can’t really get my head around wether I should even try to relate them at all using DataMapper.

I would love to know if this code is correct, how I can go about creating a comment that is related to both. If not, what structure and associations would be optimal for this scenario?

Thanks so much for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:12:49+00:00Added an answer on May 23, 2026 at 5:12 pm

    So it seems that my thinking was correct. I can relate a comment to both a user and a campaign in this way:

    # Get a user and a campaign first that we can relate to the comment
    user = User.get(user_id)
    campaign = Campaign.get(campaign_id)
    comment = Comment.new
    comment.content = "The comment's content"
    user.comments << comment # This relates the comment to a specific user
    campaign.comments << comment # This now relates the comment to a specific campaign
    comment.save # Save the comment
    

    Dangermouse’s suggestion to replace the :key => true option with :required => true also helped clean up the schema. Thanks!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a simple app in Sinatra with DataMapper. I want to
I am working on a simple budget app using Sinatra and DataMapper in Ruby.
Working in Eclipse on a Dynamic Web Project (using Tomcat (v5.5) as the app
I'm writing a delayed_job clone for DataMapper. I've got what I think is working
I'm trying to get will_paginate working with Sinatra and I have a stange problem
Well, I've finally decided that I'm not crazy. So, that leaves DataMapper. Here's what
I'm working with DataMapper and trying to use associations between models Project and Task.
I've been writing a test for a simple Datamapper class, and I know the
Working on a method to allow users to attach a doc from their Google
Working with OpenGL, I used CreateWindow( app, App Window, WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.