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 6600395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:36:59+00:00 2026-05-25T18:36:59+00:00

Ok, so this is driving me crazy. I have read the Associations article and

  • 0

Ok, so this is driving me crazy. I have read the Associations article and example and been trying to work this out for the las three days and I’m tired of this making me feel dumb, so…

How does one set up associations with DataMapper?

(I am using DM with Sinatra with SQLite3. Everything word fine for single tables with multiple values etc. It’s when I start to try to associate them that I start getting errors.)

Let’s say I have an Orchard full of Apple Trees. Each Tree has many Apples. Each Apple has many Seeds. Therefore each tree has many Seeds through its Apples

require 'sinatra'
require 'datamapper'

DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/orchard.db")

# Trees in the orchard.
class Tree
  include DataMapper::Resource
  property :id, Serial

  has n, :apples
  has n, :seeds, :through => :apples
end

# Apples on a Tree.
class Apple
  include DataMapper::Resource
  property :id, Serial

  belongs_to :tree
  belongs_to :seed
end

# Seeds in an Apple
class Seed
  include DataMapper::Resource
  property :id, Serial

  has n, :apple
  has n, :tree, :through => apple 
end

DataMapper.finalize.auto_upgrade!

Is that correct? I keep getting various errors when I try to run this. Mostly along the lines of invalid association or cannot create column NULL with value NULL etc. What am I not understanding about this relationship?

Further, Once I have a working model how do I go about getting information from it?

If there are 3 trees:

Tree.all.count 
=> 3

If there are 2 apples:

Apple.all
=>[#<Apple @id=1>, #<Apple @id=2>]  

Ok cool.
But how many Apples does Tree #2 have?
How many Seeds does Tree #4 have?
How many Apples in total?
How many Seeds in total?

Any help would be greatly appreciated.

  • 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-25T18:37:00+00:00Added an answer on May 25, 2026 at 6:37 pm

    Your model seems a bit confused:

    Let’s say I have an Orchard full of Apple Trees. Each Tree has many Apples. Each Apple has many Seeds. Therefore each tree has many Seeds through its Apples.

    So a tree has many apples, an apple belongs to a tree and has many seeds, and a seed belongs to an apple (and ultimately a single tree).

    We can almost (but not quite) take that language as it is and use it to create the associations. After a little translation to get the syntax right we get this:

    # Trees in the orchard.
    class Tree
      include DataMapper::Resource
      property :id, Serial
    
      has n, :apples    # "a tree has many apples"
      has n, :seeds, :through => :apples
    end
    
    # Apples on a Tree.
    class Apple
      include DataMapper::Resource
      property :id, Serial
    
      belongs_to :tree # "an apple belongs to a tree..."
      has n, :seeds    # "...and has many seeds"
    end
    
    # Seeds in an Apple
    class Seed
      include DataMapper::Resource
      property :id, Serial
    
      belongs_to :apple  # "and a seed belongs to an apple"
    end
    

    In your code you have seeds having multiple apples and trees, which doesn’t really make any sense.

    As for querying:

    But how many Apples does Tree #2 have?

    Assuming you mean the Tree with id == 2:

    tree_2 = Tree.get(2)
    apples_of_tree_2 = tree_2.apples # this gives an array of apples
    count_of_apples_of_tree_2 = tree_2.apples.count
    

    How many Seeds does Tree #4 have?

    The association we added to the Tree model has n, :seeds, :through => :apples means we have a seeds method available in Tree objects.

    Tree.get(4).seeds.count
    

    How many Apples in total? How many Seeds in total?

    Simply:

    Apple.count  # note singular not plural (it's a class method on Apple)
    Seed.count
    

    Try loading this new model into irb (you might need to delete your orchard.db file when you change the model), and then playing around with some of the queries and creation methods, hopefully that’ll give you a better idea of what’s going on.

    Creating associations

    (See the section "Adding To Associations" on the Associations page.)

    To add an existing Apple to a Tree:

    a_tree.apples << an_apple
    

    Note that a Tree isn’t associated with a single Apple but a collection (it has n Apples), so the method created is apples (i.e. it’s pluralized), and there’s no method apple which is why you’re seeing the no method error.

    You can also create a new Apple associated with a Tree directly:

    a_tree.apples.new    #created but not saved to database
    a_tree.apples.create #created and saved to database
    

    You can also create the association the other way round, from the Apple side:

    an_other_apple = Apple.new
    an_other_apple.tree = a_tree
    

    but you need to be careful doing it this way, as the new apple won’t show up in the a_trees collection of Apples (a_tree.apples won’t include an_other_apple). In order for it to appear you need to save the apple, and then call reload on the Tree:

    an_other_apple.save
    a_tree.reload
    

    You need to watch out with this, as you can end up with an Apple that appears to be in two Trees at the same time if you’re not careful.

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

Sidebar

Related Questions

ok this is driving me crazy. I have been trying to parse a xml
This has been driving me crazy for the past few minutes I have a
This has been driving me crazy. I think I have everything in place but
This is driving me crazy! I have read at least 5 questions on here
This is driving me crazy, I just can't find out the problem: I have
This is driving me crazy. In the past I have been able to have
This has been driving me crazy for the past 2 days, and anything close
This is driving me crazy. I have a PHP script that gets some data
This is driving me crazy. I have a very simple user control: public int?
This is driving me crazy and I can't find the answer anywhere. I have

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.