One-to-One: Undefined method build

后端 未结 3 1336
无人共我
无人共我 2020-12-28 18:27

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          


        
3条回答
  •  鱼传尺愫
    2020-12-28 19:13

    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

提交回复
热议问题