简体   繁体   English

通过Polymorphic association rails创建对象

[英]Creating object by Polymorphic association rails

I need (or I think) implement polymorphic association in my model but I have something wrong. 我需要(或者我认为)在我的模型中实现多态关联,但是我有些不对劲。 Let see my situation, it's a simple question/answers system, and the logic is the following: - a question can be ansewered by N answers. 让我们看看我的情况,这是一个简单的问题/答案系统,逻辑如下: - 一个问题可以通过N个答案回答。 - An answer can be only a "text" XOR (one or other, not both) a "picture". - 答案可能只是“文本”XOR(一个或另一个,而不是两个)“图片”。

Migrations: 迁移:

class CreateAnswers < ActiveRecord::Migration
    def change
        create_table :answers do |t|
            t.integer :question_id
            t.references :answerable, :polymorphic => true
            t.timestamps
        end
    end
end

class CreateAnswerTexts < ActiveRecord::Migration
    def change
        create_table :answer_texts do |t|
            t.text :content

            t.timestamps
        end
    end
end

class CreateAnswerPictures < ActiveRecord::Migration
    def change
        create_table :answer_pictures do |t|
            t.string :content

            t.timestamps
        end
    end
end

Models * answer.rb * 型号 * answer.rb *

class Answer < ActiveRecord::Base
    belongs_to :user_id
    belongs_to :question_id
    belongs_to :answerable, :polymorphic => true

    attr_accessible :answerable_type
end

answer_text.rb answer_text.rb

class AnswerText < ActiveRecord::Base
    TYPE = "text"

    has_one :answer, :as => :answerable

    attr_accessible :content
end

answer_picture.rb answer_picture.rb

class AnswerPicture < ActiveRecord::Base
    TYPE = "picture"

    has_one :answer, :as => :answerable

    attr_accessible :content
end

Controller answers_controller.rb: 控制器answers_controller.rb:

...
def create
    post = params[:answer]
    create_answerable(post[:answerable_type], post[:answerable])
    @answer = @answerable.answer.new()
end

private
def create_answerable(type, content)
    @answerable = ('Answer' + type.capitalize).classify.constantize.new(:content => content)
    @answerable.save
end
...

And view form (Only have these fields): 并查看表单(只有这些字段):

...
<div class="field">
<%= f.label :answerable_type %><br />
<%= select("answer", "answerable_type", Answer::Types, {:include_blank => true}) %>
</div>
<div class="field">
<%= f.label :answerable %><br />
<%= f.text_field :answerable %>
</div>
...

So, the problem is when I submit form I get this error: 所以,问题是当我提交表单时,我收到此错误:

undefined method new' for nil:NilClass app/controllers/answers_controller.rb:52:in create' undefined方法new' for nil:NilClass app/controllers/answers_controller.rb:52:in create'

Answers? 答案? :) :)

on a has_one relationship, you have to use : has_one关系中,你必须使用:

@answerable.build_answer

or 要么

@answerable.create_answer

instead of 代替

@answerable.answer.new

see the reference for more info. 有关详细信息,请参阅参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM