无法访问has_many belongs_to关系的模型Rails 5

时间:2018-03-16 03:52:09

标签: ruby-on-rails ruby simple-form

我正在尝试将交货对象的信息存储在卡车上。即使在模型的belongs_to属性上设置了外键,我似乎无法访问传递。此后,我应该能够访问rails视图中的delivery_order嵌套属性。我想要做的是绘制谷歌地图,卡车对象本身和delivery_order对象之间的距离。我写的代码如下。

错误:

<Truck id: 2, loaded_date: nil, delivery_total: "1", lon: "-73.8934515", lat: "40.8725615", warehouse_id: 1, delivery_id: 4, created_at: "2018-03-16 03:27:39", updated_at: "2018-03-16 03:27:39", current_street_address: "", current_city: "LouisVille", current_state: "Kentucky", current_country: "US", truck_driver_name: "Daniel Jolen", current_location_title: nil>
>> @truck.delivery
NoMethodError: undefined method `delivery' for #<Truck:0x000000000ac63608>
Did you mean?  delivery_id
    from (irb):51

更新2

将卡车型号设置为has_one:delivery后,我收到以下错误。

#<ActiveRecord::Relation [#<Truck id: 3, loaded_date: "2018-08-03 06:35:00", delivery_total: "1", lon: "-73.9310222", lat: "40.795386", warehouse_id: 1, delivery_id: 4, created_at: "2018-03-16 04:52:18", updated_at: "2018-03-16 04:52:18", current_street_address: "517 E 117th St", current_city: "Manhattan", current_state: "New York", current_country: "US", truck_driver_name: "Kimble Tyler", current_location_title: "Costco">]>
>> @truck = Truck.find(3)
#<Truck id: 3, loaded_date: "2018-08-03 06:35:00", delivery_total: "1", lon: "-73.9310222", lat: "40.795386", warehouse_id: 1, delivery_id: 4, created_at: "2018-03-16 04:52:18", updated_at: "2018-03-16 04:52:18", current_street_address: "517 E 117th St", current_city: "Manhattan", current_state: "New York", current_country: "US", truck_driver_name: "Kimble Tyler", current_location_title: "Costco">
  Truck Load (1.0ms)  SELECT  "trucks".* FROM "trucks" WHERE "trucks"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
>> @truck.delivery
  Delivery Load (1.0ms)  SELECT  "deliveries".* FROM "deliveries" WHERE "deliveries"."truck_id" = $1 LIMIT $2  [["truck_id", 3], ["LIMIT", 1]]
nil

delivery.rb

class Delivery < ApplicationRecord
  belongs_to :truck, class_name: 'Truck', foreign_key: :delivery_id, optional: true
  has_one :delivery_order
  has_one :order_tracker
  has_many :breads
  after_create :build_order_tracker
  # after_create :decrement_bread_stock
  accepts_nested_attributes_for :delivery_order
end

truck.rb

class Truck < ApplicationRecord
  belongs_to :warehouse, optional: true
  has_many :deliveries
  has_paper_trail on: [:update, :destroy]
  geocoded_by :truck_location, :latitude => :lat, :longitude => :lon
  after_validation :geocode, if: -> (obj) {obj.current_street_address.present? && obj.current_street_address_changed?}
  def truck_location
    [current_street_address, current_state, current_country].compact.join(', ')
  end
end

卡车架构

create_table "trucks", force: :cascade do |t|
    t.datetime "loaded_date"
    t.string "delivery_total"
    t.string "lon"
    t.string "lat"
    t.bigint "warehouse_id"
    t.bigint "delivery_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "current_street_address"
    t.string "current_city"
    t.string "current_state"
    t.string "current_country"
    t.string "truck_driver_name"
    t.string "current_location_title"
    t.index ["delivery_id"], name: "index_trucks_on_delivery_id"
    t.index ["warehouse_id"], name: "index_trucks_on_warehouse_id"
  end

trucks_controller.rb

class TrucksController < ApplicationController
  before_action :set_truck, only: %i[show edit update destroy]
  before_action :authenticate_manager!
  # GET /trucks
  # GET /trucks.json
  def index
    @trucks = Truck.all
  end

  # GET /trucks/1
  # GET /trucks/1.json
  def show; end

  # GET /trucks/new
  def new
    @truck = Truck.new
    @deliveries = Delivery.all
  end

  # GET /trucks/1/edit
  def edit; end

  # POST /trucks
  # POST /trucks.json
  def create
    @truck = current_manager.warehouse.trucks.build(truck_params)

    respond_to do |format|
      if @truck.save
        format.html { redirect_to @truck, notice: 'Truck was successfully created.' }
        format.json { render :show, status: :created, location: @truck }
      else
        format.html { render :new }
        format.json { render json: @truck.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /trucks/1
  # PATCH/PUT /trucks/1.json
  def update
    respond_to do |format|
      if @truck.update(truck_params)
        format.html { redirect_to @truck, notice: 'Truck was successfully updated.' }
        format.json { render :show, status: :ok, location: @truck }
      else
        format.html { render :edit }
        format.json { render json: @truck.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /trucks/1
  # DELETE /trucks/1.json
  def destroy
    @truck.destroy
    respond_to do |format|
      format.html { redirect_to trucks_url, notice: 'Truck was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_truck
    @truck = Truck.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def truck_params
    params.require(:truck).permit(:delivery_total, :lon, :lat, :delivery_id, :loaded_date, :truck_driver_name, :current_location_title, :current_street_address, :current_city, :current_state, :current_country)
  end
end

货车_form.html.erb

<div class="container">
  <%= simple_form_for(@truck) do |f| %>
    <%= f.error_notification %>
    <div class="form-group">
      <div class="col-3">
        <label>Loaded Date (Click Box Below)</label>
        <%= f.input :loaded_date, as: :date_time_picker, class: 'form-control', placeholder: "Tap to view calendar <i class: 'fa fa-hand-o-up'></i>", label: false %>
      </div>

    </div>
    <div class="form-group">
      <div class="col-5">
        <%= f.input :delivery_total, class: 'form-control' %>
      </div>

    </div>
    <div class="form-group">
      <div class="col-5">
        <%= f.input :current_location_title, class: 'form-control' %>
      </div>
    </div>
    <div class="form-group">
      <div class="col-5">
        <%= f.input :truck_driver_name, class: 'form-control btn btn-outline-primary' %>
      </div>

    </div>
    <div class="form-group">
      <div class="col-5">
        <%= f.input :current_street_address, class: 'form-control' %>
      </div>

    </div>
    <div class="form-group">
      <div class="col-5">
        <%= f.input :current_city, class: 'form-control' %>
      </div>

    </div>
    <div class="form-group">
      <div class="col-5">
        <%= f.input :current_state, class: 'form-control' %>
      </div>
    </div>
    <div class="form-group">
      <div class="col-5">
        <%= f.input :current_country, class: 'form-control' %>
      </div>
    </div>

    <div class="form-group">
      <div class="col-5">
        <%= f.input :delivery_id, collection: @deliveries, :label_method => lambda {|delivery| "Estimated Delivery Date: #{delivery.delivery_date} | Order Id: #{delivery.id}"}, value_method: :id, label: "Delivery Jobs", include_blank: false, prompt: 'Add a delivery to the truck'%>
      </div>

    </div>
    <div class="form-actions">
      <%= f.button :submit, class: 'btn btn-outline-success' %>
    </div>
  <% end %>
</div>



<script>
    $(document).on('turbolinks:load', function () {
        $('.form_datetime').datetimepicker({
            autoclose: true,
            todayBtn: true,
            pickerPosition: "bottom-left",
            format: 'mm-dd-yyyy hh:ii'
        });
    });
</script>

2 个答案:

答案 0 :(得分:1)

您的卡车架构的正确关系是:

class Truck < ApplicationRecord
  belongs_to :delivery

class Delivery < ApplicationRecord
  has_many :trucks

如果您希望每辆卡车有多次交货,请考虑以下架构:

create_table "deliveries", force: :cascade do |t|
  t.bigint "truck_id"

class Truck < ApplicationRecord
  has_many :deliveries

class Delivery < ApplicationRecord
  belongs_to :truck

答案 1 :(得分:0)

根据您在truck.rb和delivery.rb中的关联定义,获取与特定卡车相对应的交货方式为:

假设@truck是Truck类的实例

@truck.deliveries

这将为ActiveRecord :: Associations :: CollectionProxy提供结果。

注意:

@ truck.delivery将无法在truck.rb关联中定义为 has_many:deliveries