1

为什么我在 Bids 中选择 _form 时收到此错误?

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

request_for_proposal has_many bids bids_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>  

在投标中,我需要通过 RFP 获得“invited_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 => :rfp_venders 联系供应商。

我会担心到达 Vendors.friendly_name 的细微差别吗?

4

1 回答 1

1

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

如果你有form_for @my_instance do |f|,那么在块内f是一个表单构建器实例,与 ; 不是同一个对象@my_instance。您应该使用@my_instanceorf.object来访问 ActiveRecord 实例。

于 2013-03-09T00:45:27.763 回答