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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:10:14+00:00 2026-05-23T15:10:14+00:00

Well, I’ve finally decided that I’m not crazy. So, that leaves DataMapper. Here’s what

  • 0

Well, I’ve finally decided that I’m not crazy. So, that leaves DataMapper.

Here’s what I’m doing. I have a model Msrun which has 1 Metric.

tmp = Msrun.first_or_create # I'll skip the boring details
tmp.metric = Metric.first_or_create( {msrun_id: tmp.id}, {metric_input_file: @metricsfile} )
p tmp.metric # => #<Metric @metric_input_file=nil @msrun_id=1>
tmp.metric.metric_input_file = @metricsfile
p tmp.metric # => #<Metric @metric_input_file=#<Pathname:/home/ryanmt/Dropbox/coding/rails/metrics_site/spec/tfiles/single_metric.txt> @msrun_id=1>

So, why doesn’t this work? I’m reading http://datamapper.org/docs/create_and_destroy and doing what it shows working. This has been terribly arduous. Thanks for any help.

Update:
I still can’t figure out what is going on, but to prove I’m not insane…

puts Metric.all # => []
tmp.metric = Metric.first_or_create( {msrun_id: tmp.id}, {metric_input_file: @metricsfile} )
puts Metric.all # => [] #??????????????
tmp.metric.metric_input_file = @metricsfile
p tmp.metric # => #<Metric @metric_input_file=#<Pathname:/home/ryanmt/Dropbox/coding/rails/metrics_site/spec/tfiles/single_metric.txt> @msrun_id=1>
tmp.metric.save
puts Metric.all # => [#<Metric @metric_input_file=#<Pathname:/home/ryanmt/Dropbox/coding/rails/metrics_site/spec/tfiles/single_metric.txt> @msrun_id=1>]

So, not only is first_or_create not delivering on the behavior I expect by reading the source

def first_or_create(conditions = {}, attributes = {})
  first(conditions) || create(conditions.merge(attributes))
end

but it is also not even creating.

  • 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-23T15:10:15+00:00Added an answer on May 23, 2026 at 3:10 pm

    I’m probably missing something here (more of those boring details might help) but if the metric exists, it’s metric_input_file shouldn’t be updated, i.e., it’s only set when new. If you’re after updating then you can do

    .first_or_create(msrun_id: tmp.id).update(metric_input_file: @metricsfile)
    

    Or if not hitting the database twice is relevant, then

    m = Metric.first_or_new(msrun_id: tmp.id)
    [set..save..assign]
    

    But if it’s not being set on new models, I don’t see what would cause that from the code posted so far, more..?

    [UPDATED]

    Based on your new code, I’d say this is “a classic case” of a false DM save. I usually add the following line to an initialization section, e.g., application.rb in Rails.

    DataMapper::Model.raise_on_save_failure = true
    

    Unfortunately, the exception raised never tells you why (there’s a special place in hell for that choice, right next to people who talk in theaters.) But it’s typically one of:

    • a slightly incorrect association definition
    • a has/belongs_to that isn’t “required: false” and isn’t set
    • putting the wrong datatype into a field, e.g., a string into a decimal
    • a validation failing

    If you want to post your model definitions, the problem may be spottable there.

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

Sidebar

Related Questions

Well after much messing about I have finally got a query that gives sales
Well im here because i have a problem. i have code that was created
Well, I have to revive a question that was answered here before. I've made
Well, this is my first post here and really enjoying the site. I have
Well, this must be a silly one. Here below is a can-not-be-simpler code in
Well i have var message , which has any type of character... I replace
Well friends, I have got this query which works but is very long for
Well, the subject says it all, basically. I have a command-line utility that may
Well, I have this bit of code that is slowing down the program hugely
Well title speaks for itself, I have a footer that I expect to at

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.