Pet*_*jan 20 ruby activerecord ruby-on-rails fixtures ruby-on-rails-3
我有以下型号:
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
Run Code Online (Sandbox Code Playgroud)
每当我收到新请求时,我都会向同一地区的公司发送通知.
如何在我的灯具中进行设置,以便我可以单独测试找到合适公司的逻辑?
我试过了
region_ids: 1, 2
regions: one, two
Run Code Online (Sandbox Code Playgroud)
在companies.yml中,但都不适用于为公司分配区域.
以下是SQL生成的要点:https://gist.github.com/2713518
Fre*_*ung 34
对于
regions: one, two
Run Code Online (Sandbox Code Playgroud)
在companies.yml工作中,您需要让rails自动为区域分配ID.这是因为(为了避免在companies.yml之前必须读取regions.yml),rails会根据公司灯具的名称计算它在连接表中的ID.如果您自己分配了ID,则它们将与计算出的ID不匹配.
从您提供的sql看起来,您将区域上的ID设置为1和2,即您的regions.yml具有
one:
id: 1
name: MyString
Run Code Online (Sandbox Code Playgroud)
删除id:1,你应该没问题.您还需要更新引用区域的任何其他文件(例如request_for_proposals.yml)
region_id: 1
Run Code Online (Sandbox Code Playgroud)
同
region: one
Run Code Online (Sandbox Code Playgroud)
Rails会知道这意味着设置region_id为one你的灯具中带有标签的区域的id .
| 归档时间: |
|
| 查看次数: |
5919 次 |
| 最近记录: |