My app consists of 3 models:
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
The schema is roughly as follows:
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
I am trying to filter out data based on the input. For example someone searches for a model with height 5'8", with black eyes, and brown hair, I should display only those models by querying the database.
Therefore, I am trying to use named scopes in the model. I am not sure how do I scope the attributes of the measurement
table by writing a scope in the FashionModel
model.
I read a few resources online and from what I understood, I wrote something like
scope :eye_color, includes(:measurement).where(measurement: { eyes: "Amber" })
Though I don't want to hardcode Amber
into the eyes field, I get an error in my console when trying to access this scope. I do something like
a = FashionModel.all
a.eye_color
This gives me an and ArgumentError: wrong number of arguments (given 0, expected 1)
.
I also tried doing this
scope :eye_color, -> (eye_color) { where eyes: eye_color }
and then calling it by a.eye_color("Amber")
, which in turn gave me a NoMethodError: undefined method 'measurement' for Class
.
So basically I want to scope into the child models from the parent model. Any help is much appreciated! Thanks
Define your scope as:
scope :eye_color, ->(eye_color) {
includes(:measurements).where(measurements: {eyes: eye_color})
}
And then query with a param like:
FashionModel.eye_color("Amber")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.