user3565998
user3565998

Reputation: 1

Rails ActiveRecord::Relation error

Looking into passing a variable as value on ActiveRcord query key/column?

login = Login.where("email_address" => "[email protected]","active" => 1).select("id")
=> [#<Login id: 767>]
login = Login.where("email_address" => "[email protected]","active" => 1).select("id").class
=> ActiveRecord::Relation

admin = Admin.where("login_id"=>login).exists?

Heeeelp

Upvotes: 0

Views: 475

Answers (3)

Arup Rakshit
Arup Rakshit

Reputation: 118271

You can write as

login_ids = Login.where("email_address" => "[email protected]","active" => 1).pluck(:id)
Admin.where("login_id in (?)", login_ids).blank?

blank?

Returns true if relation is blank.

Admin.where("login_id in (?)", login_ids) gives back us ActiveRecord::Relation object, so I think we can use #blank? method.

Upvotes: 2

BroiSatse
BroiSatse

Reputation: 44685

I would do:

login = Login.where(email_address: '[email protected]', active: 1)
Admin.exists?(login_id: login.pluck(:id))

Upvotes: 2

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

It should be

admin = Admin.where("login_id" => login.first.id).exists?

Upvotes: 0

Related Questions