問題描述
模型沒有像我認為的那樣連接 (Models not connecting as I think they should be)
Why am I getting this error a _form's select within Bids?
=> undefined method `request_for_proposal' for
# <ActionView::Helpers::FormBuilder:0x002aaad671c9c8>
request_for_proposal has_many bids bids 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>
In Bids I need to go through the RFP to get "invited_vendors" so that I can later limit or restrict the vendors that can actually submit a bid.
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 | |
+‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑+‑‑‑‑‑‑‑‑‑‑+‑‑‑‑‑‑+‑‑‑‑‑+‑‑‑‑‑‑‑‑‑+‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑+
????????????????????????????????????????????????????
After I get past this annoying inability to 'see' request_for_proposals,
I'm going to need to reach Vendors via the :through => :rfp_venders.
Will I have nuances to worry about to reach Vendors.friendly_name?
參考解法
方法 1:
From the irb error you've shown, it looks like you're calling request_for_proposal
on the form builder instance, not the ActiveRecord instance itself.
If you have form_for @my_instance do |f|
, then within the block the f
is a form builder instance, not the same object as @my_instance
; you should use @my_instance
or f.object
to get to the ActiveRecord instance.
(by Ralph Smith、Brenton Fletcher)