cheeseandpepper
cheeseandpepper

Reputation: 405

Why is Model.where(id: 4) different than Model.where(:id == 4)?

When typing into the rails console

@draft_variables = DraftVariable.where(draft_id: params[:id].to_i)

it returns all rows with the matching ID, while typing

@draft_variables = DraftVariable.where(:draft_id == params[:id].to_i)

returns all rows that have any ID.

Why is this? I'm on Rails 4.1, ruby 2.1.0.

Upvotes: 1

Views: 74

Answers (1)

usha
usha

Reputation: 29359

Following statement

@draft_variables = DraftVariable.where(:draft_id == params[:id].to_i)

evaluates to

@draft_variables = DraftVariable.where(false)

Which in turn results in query

SELECT `draft_variables`.* FROM `draft_variables`

So it returns all the records

Upvotes: 3

Related Questions