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

The Archive Base Latest Questions

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

got a problem with one to one relations I have some Matches and i

  • 0

got a problem with one to one relations

I have some Matches and i want to have one score for a match.

my Match.rb

has_one :score, :dependent => :destroy

my score.rb

belongs_to :match

my scores_controller.rb

def new
@match = Match.find(params[:match_id])
@score = @match.score.new
end

def create
@match = Match.find(params[:match_id])
@score = @match.score.create(params[:score])
end

my routes.rb

resources :matches do
resources :scores
end

my scores/new.html.haml

= form_for([@match, @match.score.build]) do |f|
    = f.label :score1
    = f.text_field :score1
    %br
    = f.label :score2
    =f.text_field :score2
    %br
    = f.submit

my error that i get

undefined method `new' for nil:NilClass

i haven’t worked with one to one relations so far since I’m pretty new to RoR, any suggestions?

EDIT

edited my code to match create_score and build_score, seems to work. but now i have some kind of strange behavior.

in my score.rb

attr_accessible :score1, :score2

but when i try to invoke in my matches/show.html.haml

= @match.score.score1

i get an unknown method call or i don’t see anything at all… but if i just call

= @match.score

i get an score object returned (e.g. #)#

EDIT 2

Fix’d problem. I was calling

scores/new.haml.html

= form_for([@match, @match.create_score])

needs to be

= form_for([@match, @match.build_score])

everything works as intended.

needed to enter rails console and fetch those objects to see every :score1 :score2 was nil

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

    Use build instead of new:

    def new
        @match = Match.find(params[:match_id])
        @score = @match.build_score
    end
    

    Here are the docs for this: http://guides.rubyonrails.org/association_basics.html#belongs_to-build_association

    Similarly, in the create method, do it like this:

    def create
        @match = Match.find(params[:match_id])
        @score = @match.create_score(params[:score])
    end
    

    Docs for this: http://guides.rubyonrails.org/association_basics.html#belongs_to-create_association

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

Sidebar

Related Questions

I've got a little Problem: I want to have Buttons in some of the
I got a simple problem in SQLAlchemy. I have one model in a table,
I got problem with some special characters. I have defined the meta tag as
I have got a List<Problem> which I want to export to a column based
I have written a forum, but I've got one problem... The text from the
I got one problem at my exam for subject Principal of Programming Language. I
I'm skinning scrollbar in my flex app and got one problem. This white square
I got over a problem, I think a very specific one. I've got 2
Actually I am confronted with a Problem. I've got a .apk-File in one Package
Alright, this one (3a; sample problem with provided answer) has got me scratching my

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.