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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:27:59+00:00 2026-06-14T17:27:59+00:00

I am getting to grips with DataMapper on sqlite3 at the moment. I have

  • 0

I am getting to grips with DataMapper on sqlite3 at the moment. I have to models defined which are creating two tables: “companies” and “apps”.

Each app belongs to a company and each company many apps. I want to represent this relationship in my models but I add the “has n” and “belongs_to” methods to each class, the App class stops working when call #create on a bunch of apps, they are not inserted into the database.

If I don’t have the associations methods then everything works fine.

This is my DataMapper code:

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

class Company
    include DataMapper::Resource

    property :id, Serial
    property :company_name,
    property :company_id, Text, :unique => true

    has n, :apps
end

class App
    include DataMapper::Resource

    property :id, Serial
    property :app_id, Integer
    property :bundle_id, Text
    property :game_name, Text
    property :company_name, Text
    property :created_at, DateTime
    property :rank, Integer

    belongs_to :company
end

DataMapper.finalize.auto_upgrade!

puts 'Database and tables created'

This is the code I am using to populate my tables

companies_in_chart.each do |company|
    @add_company = Company.create(
        :company_name   => company["company_name"],
        :company_id     => company["company_id"]
    )
end
puts "Inserted companies into database"

apps_arr.each do |app|
    @new_app = App.create(
        :app_id         => app["app_id"],
        :bundle_id      => app["bundle_id"],
        :game_name      => app["game_name"],
        :company_name   => app["company_name"],
        :created_at     => app["DateTime"],
        :rank           => app["rank"]
    )
end
puts "Inserted apps into database"

EDIT: New code

#Set up database and apps table
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/app.db")

class Company
    include DataMapper::Resource

    property :id, Serial
    property :company_name, Text,    :required => true, :lazy => false
    property :company_id, Text,      :required => true, :lazy => false, :unique => true

    has n, :apps
end

class App
    include DataMapper::Resource

    property :id, Serial
    property :app_id, Integer,      :required => true
    property :bundle_id, Text,      :required => true, :lazy => false
    property :game_name, Text,      :required => true, :lazy => false
    property :company_id, Text,     :required => true, :lazy => false
    property :created_at, DateTime
    property :rank, Integer

    belongs_to :company
end

DataMapper.finalize.auto_upgrade!

puts 'Database and tables created'

#Insert apps and companies into database
apps_arr.each do |app|

    # Creates a new company based on app entry if the company does
    # not exist in the companies table
    @add_company = Company.create(
        :company_name   => app["company_name"],
        :company_id     => app["company_id"]
    )

    @add_app = App.create(
        :app_id         => app["app_id"],
        :bundle_id      => app["bundle_id"],
        :game_name      => app["game_name"],
        :company_id     => app["company_id"],
        :created_at     => app["DateTime"],
        :rank           => app["rank"]
    )
end
puts "Inserted app and companies into database"

@company = Company.first
ap @company # => #<Company @id=1 @company_name="Rovio Entertainment Ltd" @company_id="rovio">

ap @company.apps # => [] --I was hoping it would return all of Rovio's apps in the database
  • 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-06-14T17:28:00+00:00Added an answer on June 14, 2026 at 5:28 pm

    apps not created cause you have to attach a company when creating app.

    if you want to add apps not attached to any company, use this in you App model:

    belongs_to :company, :required => false
    

    to attach a company when creating app:

    #Insert apps and companies into database
    
    apps_arr.each do |app|
    
      # Creates a new company based on app entry if the company does
      # not exist in the companies table
    
      company = Company.first_or_create(
        :company_name   => app["company_name"],
        :company_id     => app["company_id"]
      )
    
      app = App.first_or_create(
        :company        => company, # you missed this
        :app_id         => app["app_id"],
        :bundle_id      => app["bundle_id"],
        :game_name      => app["game_name"],
        :company_id     => app["company_id"],
        :created_at     => app["DateTime"],
        :rank           => app["rank"]
      )
    end
    puts "Inserted app and companies into database"
    

    I successfully replicated your code on CIBox and it runs perfectly.

    See the code and live demo here

    As you can see, it creates a company and attach it to created app.

    Company.first.apps returns created app, so associations works correctly.

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

Sidebar

Related Questions

I'm currently getting to grips with mod_rewrite and have ran into a stumbling block
I'm just getting to grips with OOP PHP and while I have understood it
I'm getting to grips with PHPUnit, and have so far found it pretty easy
I'm just getting to grips with Fancybox, and have made it work ok opening
OK, I'm just getting to grips with jQuery and have a little issue. I
I'm just getting to grips with TeamCity and MSDeploy and have deployment to a
I've come from a java background, so i'm still getting to grips with some
Getting extremely confused with an adminhtml module i'm trying to write! Effectively I have
Getting message expected identifier in red for below these two statements } else if
Getting to grips with Visual Studio 2010, This compiles: var x = System.Web.Security.Membership.GetUser(); And

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.