簡體   English   中英

在關聯模型上的Ruby on Rails 4中使用命名范圍

[英]Using named scopes in Ruby on Rails 4 on associated models

我的應用程序包含3個模型:

FashionModel
Measurement
ModelProfile

class FashionModel < ActiveRecord::Base
  has_secure_password
  has_one :model_profile
  has_one :measurement
  accepts_nested_attributes_for :model_profile
  accepts_nested_attributes_for :measurement
end


class ModelProfile < ActiveRecord::Base
  belongs_to :fashion_model
end


class Measurement < ActiveRecord::Base
  belongs_to :fashion_model
end

該模式大致如下:

create_table "fashion_models", force: :cascade do |t|
    t.string   "first_name",             limit: 25
    t.string   "last_name",              limit: 25
    t.string   "email",                  limit: 255, null: false
    t.datetime "created_at",                         null: false
    t.datetime "updated_at",                         null: false
    t.string   "password_digest",        limit: 255
    t.string   "password_reset_token",   limit: 255
    t.datetime "password_reset_sent_at"
  end

  create_table "measurements", force: :cascade do |t|
    t.integer  "fashion_model_id", limit: 4
    t.decimal  "feet",                         precision: 10
    t.decimal  "inches",                       precision: 10
    t.decimal  "bust",                         precision: 10, default: 36
    t.decimal  "waist",                        precision: 10, default: 28
    t.decimal  "hips",                         precision: 10, default: 36
    t.decimal  "shoes",                        precision: 10
    t.integer  "dress",            limit: 4
    t.string   "eyes",             limit: 255
    t.string   "hair",             limit: 255
    t.datetime "created_at",                                               null: false
    t.datetime "updated_at",                                               null: false
  end

  create_table "model_profiles", force: :cascade do |t|
    t.integer  "fashion_model_id", limit: 4
    t.string   "phone_number",     limit: 255
    t.date     "birthdate"
    t.text     "bio",              limit: 65535
    t.string   "location",         limit: 255,                  default: "Venice"
    t.string   "gender",           limit: 255
    t.decimal  "rate",                           precision: 10, default: 100
    t.string   "profile_picture",  limit: 255
    t.datetime "created_at",                                                       null: false
    t.datetime "updated_at",                                                       null: false
  end

  add_index "model_profiles", ["fashion_model_id"], name: "index_model_profiles_on_fashion_model_id", using: :btree

  add_foreign_key "bookings", "fashion_models"
  add_foreign_key "fashion_model_photos", "fashion_models"
end

我正在嘗試根據輸入過濾掉數據。 例如,有人搜索身高5'8“,黑眼睛,棕色頭發的模型,我應該通過查詢數據庫僅顯示那些模型。

因此,我試圖在模型中使用命名作用域。 我不確定如何通過在FashionModel模型中編寫作用域來限制measurement表的屬性。

我在線閱讀了一些資源,據我了解,我寫了類似

scope :eye_color, includes(:measurement).where(measurement: { eyes: "Amber" })

盡管我不想將Amber硬編碼到eyes字段中,但是在嘗試訪問該作用域時,控制台中出現錯誤。 我做類似的事情

a = FashionModel.all
a.eye_color

這給了我一個and ArgumentError: wrong number of arguments (given 0, expected 1)

我也嘗試這樣做

scope :eye_color, -> (eye_color) { where eyes: eye_color }

然后通過a.eye_color("Amber")調用它,這又給了我NoMethodError: undefined method 'measurement' for Class

因此,基本上我想將范圍從父模型擴展到子模型。 任何幫助深表感謝! 謝謝

將范圍定義為:

scope :eye_color, ->(eye_color) {
  includes(:measurements).where(measurements: {eyes: eye_color}) 
}

然后使用以下參數進行查詢:

FashionModel.eye_color("Amber")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM