Ruby on rails 模型(ActiveRecord)中表的主键未知

Ruby on rails 模型(ActiveRecord)中表的主键未知,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我正在尝试一个搜索表单,它将采用一个或多个可选参数并相应地查询我的模型(事件)。到目前为止,我从两个参数开始:关键字和标记 事件具有并属于多个标记。标记具有并属于许多事件。您可以在此处看到这些模型: class Event < ApplicationRecord has_many :user_event_relationships has_many :map_markers has_many :users, through: :user_event_relation

我正在尝试一个搜索表单,它将采用一个或多个可选参数并相应地查询我的模型(事件)。到目前为止,我从两个参数开始:关键字和标记

事件具有并属于多个标记。标记具有并属于许多事件。您可以在此处看到这些模型:

class Event < ApplicationRecord
    has_many :user_event_relationships
    has_many :map_markers
    has_many :users, through: :user_event_relationships
    has_and_belongs_to_many :tags
    has_one_attached :picture
    validates :name, :location, :date_to, :date_from, presence: true
    validate :valid_date_range_required, :valid_date, :image_type

    geocoded_by :location

    def image_type
        if picture.attached? && !picture.content_type.in?(%('image/jpeg image/png'))
            errors.add(:picture, "needs to be a jpeg or png!")
        end
    end

    def valid_date_range_required
        if (date_to.present? && date_from.present?) && (date_to < date_from)
            errors.add(:date_to, "Must be later than the from date!")
        end
    end

    def valid_date
        if date_from.present? && (date_from < DateTime.now)
            errors.add(:date_from, "Starting date cannot be in the past!")
        end
    end

    scope :with_name, proc { | name|
        if name.present?
            where("lower(name) like ?", "%#{name.downcase}%")
        end
    }

    scope :with_tag, proc { | tag|
        if tag.present?
            joins(:tags).where(tags: {name: Tag.find(tag).name})
        end
    }

    def self.filter(params)
        with_name(params[:keyword]).with_tag(params[:tags])
    end

    #def self.filter(params)
    #    #params.permit(SUPPORTED_FILTERS).to_hash.reduce(all) do |scope, (key, value)|
    #    #    value.present? ? scope.send(key, value) : scope
    #    #end
    #    debugger
    #    events = Event
    #    params.permit(SUPPORTED_FILTERS).to_hash.reduce(all) do |scope, (key, value)|
    #        debugger
    #        if key.to_sym == :keyword
    #            puts "keyword"
    #           events.where("lower(name) like ?", "%#{value.downcase}%")
    #        end
    #        if key.to_sym == :tags
    #            puts "tags"
    #            events.joins(:tags).where('tags.name = ?', Tag.find_by(name: value))
    #        end
    #        scope
    #    end
    #end
end
我的目标是能够基于单个标记名筛选事件。例如,如果我有“基本事件1”和“基本事件2”,第一个有“标记A”,第二个有“标记B”,如果我查询包含“基本”和“标记A”的名称的事件,我只会得到第一个事件

我的self.filter命令引发以下错误:

*** ActiveRecord::UnknownPrimaryKey Exception: Unknown primary key for table events_tags in model Event::HABTM_Tags. 

任何帮助都将不胜感激

Tag.find仅接受主键,这是约定中的ID列。 除非您给它一个PK,否则它会引发错误。
我猜您可能给了它一个要查询的名称字符串。

连接(:标记).where(标记:{name:Tag.find(Tag.name})
#改为
联接(:标记)。其中(标记:{name:name})
很明显你在where子句中用错了find, 因为两者都使用
:find
:find_by.*
方法,如果找到了一条记录,则应仅返回一条记录,并精确返回。这与您在where()中编写查询的方式相矛盾

  create_table "events", force: :cascade do |t|
    t.string "name"
    t.integer "tags"
    t.datetime "date_from"
    t.datetime "date_to"
    t.string "location"
    t.decimal "latitude", precision: 10, scale: 6
    t.decimal "longitude", precision: 10, scale: 6
    t.string "description"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "tags", force: :cascade do |t|
    t.string "photo_url"
    t.string "name"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

  create_table "events_tags", id: false, force: :cascade do |t|
    t.bigint "event_id", null: false
    t.bigint "tag_id", null: false
    t.index ["event_id", "tag_id"], name: "index_events_tags_on_event_id_and_tag_id"
    t.index ["tag_id", "event_id"], name: "index_events_tags_on_tag_id_and_event_id"
  end
*** ActiveRecord::UnknownPrimaryKey Exception: Unknown primary key for table events_tags in model Event::HABTM_Tags.