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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:07:38+00:00 2026-06-18T08:07:38+00:00

When I try to this code, I get an IllegalContextError at the self.save… line.

  • 0

When I try to this code, I get an IllegalContextError at the “self.save…” line. Can you tell me what I’m doing wrong?

I would just call the create method on Player without messing around with initialize, but I want a related week object to be created as part of the initialization.

require 'data_mapper'

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

class Player

    include DataMapper::Resource
    property :name, String, :key => true
    property :sport, String

    has n, :weeks

    def initialize(name, sport, week)
        self.save(:name => name, :sport => sport)
        self.weeks.create(:id => "#{name}#{week}", :score => 0)
    end

end

class Week

    include DataMapper::Resource
    property :id, String, :key => true
    property :week, Integer
    property :score, Integer

    belongs_to :player

end

DataMapper.finalize.auto_migrate!

Player.new("jack", "golf", 5)
  • 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-18T08:07:39+00:00Added an answer on June 18, 2026 at 8:07 am

    I understand that this is probably not the best way, so before you shoot my method down, please provide a better solution. I will probably accept your answer 🙂

    It seems like the IllegalContextError is originating from the data_mapper validators.

    The data_mapper docs on validators doesn’t provide much info for a newbie to understand context AND in relation to validators.

    Here is my hacky workaround. I override the validators by using the bang operator (!). The solution is as follows.

    require 'data_mapper'
    
    DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/prod.db")
    
    class Player
    
        include DataMapper::Resource
        property :name, String, :key => true
        property :sport, String
    
        has n, :weeks
    
        def initialize(name, sport, week)
            self[:name] = name
            self[:sport] = sport
            self[:week] = week
            self.save!
            self.weeks.create(:id => "#{name}#{week}", :score => 0)
        end
    
    end
    
    class Week
    
        include DataMapper::Resource
        property :id, String, :key => true
        property :week, Integer
        property :score, Integer
    
        belongs_to :player
    
    end
    
    DataMapper.finalize.auto_migrate!
    
    Player.new("jack", "golf", 5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I try to run this code, I get incorrect maximums and minimums. Could
This Java code compiles fine, but when I try to run it I get:
How can I change the padding of a TableLayout programmatic. I try this code,
I have a problem to get my window size, I try this code: Javascript
So when I try this code (one line at a time): BACKUP DATABASE [test]
when i try to insert this code i get an mysql error syntax CREATE
When I try this code: class MyStuff: def average(a, b, c): # Get the
When i try to run this code i get the error: Warning: mysql_fetch_array() expects
I only get the TCP message when I try this code: from socket import
I would like to try this code: public struct Direction { private int _azimuth;

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.