I am having trouble with associations in the following code.
The error I’m getting is a comment on the last line of code.
Edit: I simplified the code…
require 'rubygems'
require 'data_mapper' # requires all the gems listed above
require 'pp'
DataMapper.setup(:default, 'sqlite:///Users/chris/Dropbox/HawkEye-DB Test/store.sqlite')
class Manufacturer
include DataMapper::Resource
property :id, Serial
property :name, String
has n, :products
end
class Product
include DataMapper::Resource
property :id, Serial
property :name, String
belongs_to :manufacturer
has 1, :productoptionset
end
class Productoptionset
include DataMapper::Resource
property :id, Serial
property :name, String
belongs_to :product
end
DataMapper.auto_migrate!
# Make some manufactureres
gortex = Manufacturer.create(:name => 'Gortex')
garmin = Manufacturer.create(:name => 'Garmin')
gps = garmin.products.create(:name => 'GPS Unit')
samegps = Product.get(1)
pp samegps.productoptionset.create # undefined method `create' for nil:NilClass (NoMethodError)
createis a class method (kind of like a static method in Java) so it can’t be called on instances (or non instances in this case) 🙂You could create your objects like this