在 rails 中保存多态模型
Saving Polymorphic model in rails
我有一个多态模型;结果, belongs_to result_table, polymorphic: true
我正在与其他两个模型一起使用;团队和运动,通过一对一的关系及其运作。由于一对一的关系,我有一个回调函数,after_create :build_result_table
,我用它来设置和保存数据库中结果 table 中的 result_table_id
和 result_table_type
。我遇到的问题是输入保存在 result_table_type
中,它是 VARCHAR,但它似乎将输入保存为整数
def build_result_table
Result.create(result_table_id: self.id, result_table_type: self)
end
我认为问题可能出在 result_table_type: self
,但我尝试 result_table_type: self.class.name
保存 class 的名称,但出现错误。关于在 result_table_type
列中保存唯一值的任何建议..
编辑
我正在添加所涉及的模型,虽然我已经按照@Mohammad 在评论中所说的那样手动完成了保存,但也将不胜感激,如果可以向我展示如何询问 rails自动保存值。
APP/MODEL/SPORT.RB
class Sport < ActiveRecord::Base
attr_accessible :sport_name, :sport_id, :result_attributes, :result_table_id
has_one :result, as: :result_table
after_create :build_result_table
accepts_nested_attributes_for :result
def build_result_table
Result.create(result_table_id: self.id, result_table_type: self.class.name)
end
end
APP/MODEL/TEAM.RB
class Team < ActiveRecord::Base
has_one :result, as: :result_table
attr_accessible :teamname, :color, :result_attributes
after_create :build_result_table
accepts_nested_attributes_for :result
def build_result_table
Result.create(result_table_id: self.id, result_table_type: self.class.name)
end
end
APP/MODEL/RESULT.RB
class Result < ActiveRecord::Base
attr_accessible :sport_id,:team_id, :result_table_id, :result_table_type
belongs_to :result_table, polymorphic: true
end
# Either one of these
Result.create(result_table: self)
Result.create(result_table_id: self.id, result_table_type: self.class.to_s)
我有一个多态模型;结果, belongs_to result_table, polymorphic: true
我正在与其他两个模型一起使用;团队和运动,通过一对一的关系及其运作。由于一对一的关系,我有一个回调函数,after_create :build_result_table
,我用它来设置和保存数据库中结果 table 中的 result_table_id
和 result_table_type
。我遇到的问题是输入保存在 result_table_type
中,它是 VARCHAR,但它似乎将输入保存为整数
def build_result_table
Result.create(result_table_id: self.id, result_table_type: self)
end
我认为问题可能出在 result_table_type: self
,但我尝试 result_table_type: self.class.name
保存 class 的名称,但出现错误。关于在 result_table_type
列中保存唯一值的任何建议..
编辑
我正在添加所涉及的模型,虽然我已经按照@Mohammad 在评论中所说的那样手动完成了保存,但也将不胜感激,如果可以向我展示如何询问 rails自动保存值。
APP/MODEL/SPORT.RB
class Sport < ActiveRecord::Base
attr_accessible :sport_name, :sport_id, :result_attributes, :result_table_id
has_one :result, as: :result_table
after_create :build_result_table
accepts_nested_attributes_for :result
def build_result_table
Result.create(result_table_id: self.id, result_table_type: self.class.name)
end
end
APP/MODEL/TEAM.RB
class Team < ActiveRecord::Base
has_one :result, as: :result_table
attr_accessible :teamname, :color, :result_attributes
after_create :build_result_table
accepts_nested_attributes_for :result
def build_result_table
Result.create(result_table_id: self.id, result_table_type: self.class.name)
end
end
APP/MODEL/RESULT.RB
class Result < ActiveRecord::Base
attr_accessible :sport_id,:team_id, :result_table_id, :result_table_type
belongs_to :result_table, polymorphic: true
end
# Either one of these
Result.create(result_table: self)
Result.create(result_table_id: self.id, result_table_type: self.class.to_s)