Ruby on rails RubyonRails无法保存有很多关系
我有两种型号Ruby on rails RubyonRails无法保存有很多关系,ruby-on-rails,rails-activerecord,Ruby On Rails,Rails Activerecord,我有两种型号行业匹配器和员工休息室 模型工业匹配器 class IndustryMatcher < ActiveRecord::Base has_many :industries, class_name: 'Staffroom', conditions: ["staffroom_type = 'Industry'"] 我可以为model Industry Matcher保存数据,但根本无法保存关系 这是链接这两者的模型类 class IndustryMatcherStaffr
行业匹配器
和员工休息室
模型工业匹配器
class IndustryMatcher < ActiveRecord::Base
has_many :industries, class_name: 'Staffroom', conditions: ["staffroom_type = 'Industry'"]
我可以为model Industry Matcher保存数据,但根本无法保存关系
这是链接这两者的模型类
class IndustryMatcherStaffroom < ActiveRecord::Base
attr_accessible :industry_id, :staffroom_id
belongs_to :industry_matcher
belongs_to :staffroom
end
class IndustryMatcherStaffroom
如果有人能告诉我我在这里错过了什么,以及为什么这段关系没有得到挽救,我将不胜感激
当我试图编辑记录时,保存记录后,我看到以下错误
ActiveRecord::语句在/admin/industry\u matchers/11/edit处无效
PG::UndefinedColumn:错误:column staffrooms.industry\u matcher\u id
不存在第1行:…affrooms“其中有“staffrooms”。“已删除”=
f和staffroom。。。
^:从“staffrooms”中的“staffrooms”中选择“staffrooms”.id,“staffrooms”。“已删除”
=“f”和“员工房间”。“行业匹配者id”=11和(员工房间类型=“行业”)
您的关联不完整,必须使用
has\u many to:
告诉您的模型使用联接表,如下所示:
class IndustryMatcher < ActiveRecord::Base
has_many :industry_matcher_staffrooms
has_many :industries, through: :industry_matcher_staffrooms, class_name: 'Staffroom', conditions: ["staffroom_type = 'Industry'"]
class Staffroom < ActiveRecord::Base
has_many :industry_matcher_staffrooms
has_many :industry_matchers, through: :industry_matcher_staffrooms
您可以在中阅读有关关联的更多信息。谢谢,但这会给出一个错误名称error-未定义的局部变量或方法“industry\u matcher\u staffrooms”,我甚至尝试在上面添加has\u many:industry\u matcher\u staffroomsit@Saadia对不起,我错过了
:行业匹配人员办公室中的:
。查看更新后的答案。@Saadia不需要额外的有很多:行业匹配者员工工作室
,只需使用通过::行业匹配者员工工作室
。谢谢,我现在遇到了未定义的方法“klass”,没有:NilClass@Saadia我的错,你需要的有很多:行业匹配者\u员工室
;请检查更新的答案。
class IndustryMatcherStaffroom < ActiveRecord::Base
attr_accessible :industry_id, :staffroom_id
belongs_to :industry_matcher
belongs_to :staffroom
end
class IndustryMatcher < ActiveRecord::Base
has_many :industry_matcher_staffrooms
has_many :industries, through: :industry_matcher_staffrooms, class_name: 'Staffroom', conditions: ["staffroom_type = 'Industry'"]
class Staffroom < ActiveRecord::Base
has_many :industry_matcher_staffrooms
has_many :industry_matchers, through: :industry_matcher_staffrooms
PG::UndefinedColumn: ERROR: column staffrooms.industry_matcher_id does not exist