ruby-on-rails – 如何设置一个has_and_belongs_to_many关系的灯
发布时间:2020-12-16 20:23:30 所属栏目:百科 来源:网络整理
导读:我有以下型号: class Company ActiveRecord::Base has_and_belongs_to_many :regionsclass Region ActiveRecord::Base has_many :requests has_and_belongs_to_many :companiesclass RequestForProposals ActiveRecord::Base belongs_to :region 每当我收到
我有以下型号:
class Company < ActiveRecord::Base has_and_belongs_to_many :regions class Region < ActiveRecord::Base has_many :requests has_and_belongs_to_many :companies class RequestForProposals < ActiveRecord::Base belongs_to :region 每当我收到一个新的请求,我想向在同一地区活跃的公司发送通知. 我如何将其设置在我的装置中,以便我可以单元测试寻找合适公司的逻辑? 我试过了 region_ids: 1,2 regions: one,two 在company.yml中,但两者都不适用于向公司分配地区. 这是SQL生成的一个要点:https://gist.github.com/2713518 解决方法
对于
regions: one,two 在company.yml工作中,您需要让rails自动为区域分配ids.这是因为(为了避免在company.yml之前读取regions.yml)rails将从公司灯具的名称中计算连接表中的ID.如果您自己分配了ID,则它们将不会与计算的ID匹配. 从您提供的sql中,您将将区域上的ids设置为1和2,即您的Regions.yml具有 one: id: 1 name: MyString 删除id:1,你应该可以.您还需要更新引用区域的任何其他文件(例如request_for_proposals.yml)替换 region_id: 1 同 region: one Rails将会意识到将region_id设置为您的灯具中带有标签1的区域的ID. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |