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