模型没有像我认为的那样连接

时间:2013-03-09 00:39:15

标签: ruby-on-rails has-many-through has-many belongs-to

为什么我在Bids中选择_form的这个错误?

=>  undefined method `request_for_proposal' for  
# <ActionView::Helpers::FormBuilder:0x002aaad671c9c8>  

request_for_proposal has_many 出价
出价 belongs_to request_for_proposal

====================================================
Inside views/bids/_form.html.erb

  <div class="field">  
    <%= bid.label :vendor_name %>  
    <%= bid.select :vendor_id, bid.request_for_proposal.vendors.collect { |rfpvi|  
        [rfpvi.friendly_name + "  :  " + rfpvi.rfp_number, rfpvi.id] } %>  
  </div>  

在Bids中,我需要通过RFP获取“invite_vendors”,以便我以后可以限制或限制实际提交出价的供应商。

class Bid < ActiveRecord::Base

  attr_accessible :cost, :request_for_proposal_id, :vendor_id

  belongs_to :request_for_proposal
  belongs_to :vendor
------------------------------------------------

mysql> desc bids;
+-------------------------+---------------+------+-----+---------+----------------+
| Field                   | Type          | Null | Key | Default | Extra          |
+-------------------------+---------------+------+-----+---------+----------------+
| id                      | int(11)       | NO   | PRI | NULL    | auto_increment |
| cost                    | decimal(10,0) | YES  |     | NULL    |                |
| request_for_proposal_id | int(11)       | YES  | MUL | NULL    |                |
| vendor_id               | int(11)       | YES  | MUL | NULL    |                |
| contact_id              | int(11)       | YES  | MUL | NULL    |                |
| created_at              | datetime      | NO   |     | NULL    |                |
| updated_at              | datetime      | NO   |     | NULL    |                |
+-------------------------+---------------+------+-----+---------+----------------+  

================================================
class RequestForProposal < ActiveRecord::Base

  attr_accessible   :rfp_number, :name, :description, :title,
                    :contact_id, :business_owner_id

  has_many   :bids
  has_many   :rfp_vendors
  has_many   :vendors, :through => :rfp_vendors

  has_and_belongs_to_many   :business_owners, :class_name => :Contact,
                            :foreign_key => :business_owner_id
  has_and_belongs_to_many   :evaluators,      :class_name => :Contact,
                            :foreign_key => :evaluator_id
 ----------------------------------------------- 

mysql> desc request_for_proposals;
+----------------------------+--------------+------+-----+---------+----------------+
| Field                      | Type         | Null | Key | Default | Extra          |
+----------------------------+--------------+------+-----+---------+----------------+
| id                         | int(11)      | NO   | PRI | NULL    | auto_increment |
| rfp_number                 | varchar(255) | NO   |     | NULL    |                |
| contact_id                 | int(11)      | YES  |     | NULL    |                |
| vendor_id                  | int(11)      | YES  |     | NULL    |                |
| name                       | varchar(255) | NO   |     | NULL    |                |
| description                | varchar(255) | YES  |     | NULL    |                |
| title                      | varchar(255) | NO   |     | NULL    |                |
| bid_id                     | int(11)      | YES  |     | NULL    |                |
| business_owner_id          | int(11)      | YES  |     | NULL    |                |
| evaluator_id               | int(11)      | YES  |     | NULL    |                |
| created_at                 | datetime     | NO   |     | NULL    |                |
| updated_at                 | datetime     | NO   |     | NULL    |                |
+----------------------------+--------------+------+-----+---------+----------------+

================================================
class Vendor < ActiveRecord::Base

    attr_accessible :friendly_name

    has_many :rfp_vendors
    has_many :request_for_proposals, :through => :rfp_vendors
    has_many :bids
------------------------------------------------

mysql> desc vendors;
+-------------------------+--------------+------+-----+---------+----------------+
| Field                   | Type         | Null | Key | Default | Extra          |
+-------------------------+--------------+------+-----+---------+----------------+
| id                      | int(11)      | NO   | PRI | NULL    | auto_increment |
| friendly_name           | varchar(255) | YES  |     | NULL    |                |
| created_at              | datetime     | NO   |     | NULL    |                |
| updated_at              | datetime     | NO   |     | NULL    |                |
+-------------------------+--------------+------+-----+---------+----------------+

================================================
================================================

mysql> desc bids_contacts;
+------------+---------+------+-----+---------+-------+
| Field      | Type    | Null | Key | Default | Extra |
+------------+---------+------+-----+---------+-------+
| bid_id     | int(11) | NO   |     | NULL    |       |
| contact_id | int(11) | NO   |     | NULL    |       |
+------------+---------+------+-----+---------+-------+

mysql> desc bids_vendors;
+-----------+---------+------+-----+---------+-------+
| Field     | Type    | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| bid_id    | int(11) | NO   |     | NULL    |       |
| vendor_id | int(11) | NO   |     | NULL    |       |
+-----------+---------+------+-----+---------+-------+

mysql> desc rfp_vendors;
+-------------------------+----------+------+-----+---------+----------------+
| Field                   | Type     | Null | Key | Default | Extra          |
+-------------------------+----------+------+-----+---------+----------------+
| id                      | int(11)  | NO   | PRI | NULL    | auto_increment |
| vendor_id               | int(11)  | YES  |     | NULL    |                |
| request_for_proposal_id | int(11)  | YES  |     | NULL    |                |
| created_at              | datetime | NO   |     | NULL    |                |
| updated_at              | datetime | NO   |     | NULL    |                |
+-------------------------+----------+------+-----+---------+----------------+

??????????????????????????????????????????????? ?????

在我遇到这种烦人的“看不到'request_for_proposals,

之后

我需要通过以下方式联系供应商:through =&gt; :rfp_venders

我是否有细微差别担心要到达Vendors.friendly_name?

1 个答案:

答案 0 :(得分:1)

根据您显示的irb错误,您似乎在表单构建器实例上调用request_for_proposal,而不是ActiveRecord实例本身。

如果你有form_for @my_instance do |f|,那么在块中,f是表单构建器实例,而不是@my_instance的对象;您应该使用@my_instancef.object来访问ActiveRecord实例。