From 2491bfa0e80725d2e4d14ae3dda0b5bb50b6cc51 Mon Sep 17 00:00:00 2001 From: zainforbjs Date: Fri, 12 Sep 2025 19:11:01 +0500 Subject: [PATCH 1/2] docs: fix timestamp properties, correct function names, replace scopes with custom finders Updated the docs with the correct usage of timeStampOnCreateProperty and timeStampOnUpdateProperty. Update wrong arguments and fixed function names. Removed the use of scopes and used custom finder methods. --- .../wheels/standards/code-style/cfml-style.md | 4 +-- .../wheels/standards/wheels-cli-reference.md | 6 ++-- .../wheels/standards/wheels-model-patterns.md | 28 +++++++++++++------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/project_types/wheels/standards/code-style/cfml-style.md b/project_types/wheels/standards/code-style/cfml-style.md index d82a3c39..279fc266 100644 --- a/project_types/wheels/standards/code-style/cfml-style.md +++ b/project_types/wheels/standards/code-style/cfml-style.md @@ -197,8 +197,8 @@ component extends="Model" { softDelete(true); // Automatic timestamps - timeStampOnCreate(true); - timeStampOnUpdate(true); + set(timeStampOnCreateProperty="createdAt"); + set(timeStampOnUpdateProperty="updatedAt"); } /** diff --git a/project_types/wheels/standards/wheels-cli-reference.md b/project_types/wheels/standards/wheels-cli-reference.md index 6ce73add..1193e73f 100644 --- a/project_types/wheels/standards/wheels-cli-reference.md +++ b/project_types/wheels/standards/wheels-cli-reference.md @@ -272,7 +272,7 @@ component extends="wheels.migrator.Migration" { t.string(columnName="firstName"); t.string(columnName="lastName"); t.string(columnName="email"); - t.boolean(columnName="isActive", defaultValue=true); + t.boolean(columnName="isActive", default=true); t.timestamps(); // Adds createdAt, updatedAt t.create(); } @@ -291,8 +291,8 @@ component extends="wheels.migrator.Migration" { component extends="wheels.migrator.Migration" { function up() { transaction { - addIndex(table="users", columns="email", unique=true); - addIndex(table="posts", columns="userId,createdAt"); + addIndex(table="users", columnNames="email", unique=true); + addIndex(table="posts", columnNames="userId,createdAt"); } } diff --git a/project_types/wheels/standards/wheels-model-patterns.md b/project_types/wheels/standards/wheels-model-patterns.md index 46d45338..576a5490 100644 --- a/project_types/wheels/standards/wheels-model-patterns.md +++ b/project_types/wheels/standards/wheels-model-patterns.md @@ -85,7 +85,7 @@ function config() { ### Custom Validations ```cfml function config() { - validates(property="password", method="validatePasswordStrength"); + validate(property="password", method="validatePasswordStrength"); } private void function validatePasswordStrength() { @@ -155,18 +155,30 @@ stats = model("Order").findAll( ); ``` -## Scopes +## Reusable Queries + +### Custom Finder Methods +In Wheels, models don’t support `scope()` like Rails. +Instead, you can define **custom finder methods** that wrap `findAll()` or `findOne()` with your common conditions. -### Named Scopes ```cfml -function config() { - scope(name="active", where="status = 'active'"); - scope(name="recent", where="createdAt > #createDate()#", order="createdAt DESC"); +function findActive() { + return findAll( + where = "status = 'active'" + ); } +function findRecent() { + return findAll( + where = "createdAt > #createDate()#", + order = "createdAt DESC" + ); +} + + // Usage -activeUsers = model("User").active().findAll(); -recentOrders = model("Order").recent().findAll(); +activeUsers = model("User").findActive(); +recentUsers = model("User").findRecent(); ``` ## Data Manipulation From 267a22d5cc58d7d8619facbc31a6f9d4a975e545 Mon Sep 17 00:00:00 2001 From: zainforbjs Date: Mon, 15 Sep 2025 16:31:53 +0500 Subject: [PATCH 2/2] Remove the use of whereParams argument in ORM functions --- .../wheels/standards/code-style/cfml-style.md | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/project_types/wheels/standards/code-style/cfml-style.md b/project_types/wheels/standards/code-style/cfml-style.md index 279fc266..54f3b0db 100644 --- a/project_types/wheels/standards/code-style/cfml-style.md +++ b/project_types/wheels/standards/code-style/cfml-style.md @@ -212,7 +212,7 @@ component extends="Model" { * Check if user has specific role */ function hasRole(required string roleName) { - return this.roles().exists(where="name = ?", whereParams=[arguments.roleName]); + return this.roles().exists(where="name = '#arguments.roleName#'"); } /** @@ -358,8 +358,7 @@ function create() { ```cfml // Finding records users = model("User").findAll( - where="isActive = ? AND createdAt > ?", - whereParams=[true, DateAdd("d", -30, Now())], + where="isActive = true AND createdAt > '#DateAdd("d", -30, Now())#'", order="lastName ASC, firstName ASC", include="profile,posts", page=1, @@ -590,14 +589,13 @@ component extends="Controller" { // Efficient data loading with specific selects users = model("User").findAll( select="id, firstName, lastName, email", - where="isActive = ?", - whereParams=[true], + where="isActive = true", include="profile", // Eager load to avoid N+1 order="lastName ASC" ); // Use exists() instead of count() for boolean checks -hasActiveUsers = model("User").exists(where="isActive = ?", whereParams=[true]); +hasActiveUsers = model("User").exists(where="isActive = true"); // Pagination for large datasets pagedUsers = model("User").findAll( @@ -664,14 +662,12 @@ function create() { ```cfml // Always use parameterized queries users = model("User").findAll( - where="lastName = ? AND isActive = ?", - whereParams=[params.lastName, true] // Parameters are automatically escaped + where="lastName = '#params.lastName#' AND isActive = true" ); // For complex queries users = model("User").findAll( - where="firstName LIKE ? OR lastName LIKE ?", - whereParams=["%#params.search#%", "%#params.search#%"] + where="firstName LIKE '%#params.search#%' OR lastName LIKE '%#params.search#%'" ); ``` @@ -746,8 +742,7 @@ function processData() { // DO: Use parameterized queries users = model("User").findAll( - where="name = ?", - whereParams=[params.name] + where="name = '#params.name#'" ); ```