繁体   English   中英

无法访问has_many当属关系关系模型Rails 5

[英]Unable to access model for has_many belongs_to relationship Rails 5

我正在尝试将送货对象的信息存储在卡车本身上。 即使外键是在模型的belongs_to属性上设置的,我似乎也无法访问交付。 之后,我应该能够在rails视图内访问delivery_order nested属性。 我想做的是在Google地图上绘制图,即卡车对象本身与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>

卡车方案的正确关系是:

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

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

假设@truck是Truck类的实例

@truck.deliveries

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

注意:

@ truck.delivery无法像在truck.rb中那样工作。关联定义为has_many:deliveries

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM