在rails中没有相反的方式加入工作吗?

时间:2018-02-23 04:17:49

标签: ruby-on-rails join activerecord

我正在尝试在这两个表上运行join。 这似乎有效

 BIT_SEL_TBL_INFO.joins(:bit_sel_tbls)

但它似乎并没有相反的方式。

BIT_SEL_TBL.joins(:bit_sel_tbl_infos)

显示

ActiveRecord::ConfigurationError: Can't join 'BIT_SEL_TBL' to association named 'bit_sel_tbl_infos'; perhaps you misspelled it?


#bit_sel_tbl_info.rb
    class BIT_SEL_TBL_INFO < ExternalDbAccess
        self.table_name = "BIT_SEL_TBL_INFO"
        has_many :bit_sel_tbls, class_name: "BIT_SEL_TBL", primary_key: "BIT_SEL_TBL_NO", foreign_key: "BIT_SEL_TBL_NO"
    end

#bit_sel_tbl.rb
class BIT_SEL_TBL < ExternalDbAccess
    self.table_name = "BIT_SEL_TBL"
    self.primary_key = 'BIT_SEL_TBL_NO', 'FIELD_NO'
    belongs_to :bit_sel_tbl_info, class_name: "BIT_SEL_TBL_INFO", primary_key: "BIT_SEL_TBL_NO", foreign_key: "BIT_SEL_TBL_NO"
end

1 个答案:

答案 0 :(得分:1)

哦,noob错误 不得不这样做:bit_sel_tbl_info而不是:bit_sel_tbl_infos,因为它是belongs_to

BIT_SEL_TBL.joins(:bit_sel_tbl_info)