模型沒有像我認為的那樣連接 (Models not connecting as I think they should be)


問題描述

模型沒有像我認為的那樣連接 (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 SmithBrenton Fletcher)

參考文件

  1. Models not connecting as I think they should be (CC BY‑SA 3.0/4.0)

#has-many #belongs-to #ruby-on-rails #has-many-through






相關問題

Ruby on Rails - Has_many,批量分配問題 (Ruby on Rails - Has_many, Mass Assignment issue)

如何在 hasMany 關聯中使用 CakePHP 2 查找? (How to use CakePHP 2 find in a hasMany association?)

模型沒有像我認為的那樣連接 (Models not connecting as I think they should be)

CakePhp find không tìm nạp hasMany mối quan hệ (CakePhp find doesn't fetch hasMany relationships)

創建關聯後,控制器中的 Ruby on rails NoMethodError (Ruby on rails NoMethodError in controller after create an association)

是否可以以兩種不同的方式關聯模型? (Is it possible to associate models in two different ways?)

在 DetailView yii2 中顯示 HAS_MANY 關係數據 (Display HAS_MANY relation data in DetailView yii2)

Rails - 檢查 has_many 關聯中是否存在記錄 (Rails - check if record exists in has_many association)

在 Rails 中,我可以在另一個模型的模型上設置 has_many 關係嗎? (In Rails, can I set a has_many relation on a model from another model?)

從大於 0 開始計數器緩存 (Starting a counter cache from greater than 0)

如何使用 has_many 關聯在 Rails 中排序? (How to sort in rails with has_many association?)

如何將 ActiveModel::Serializer :has_many 關聯的密鑰格式(即駝峰式)指定為一次性選項(不是全局配置)? (How to specify the key format (i.e. camelcase) for an ActiveModel::Serializer :has_many association as a one-off option (not global config)?)







留言討論