Hrishikesh Sardar
Hrishikesh Sardar

Reputation: 2917

undefined method `user_id' for #<ActiveRecord::Relation:0x007f8da82da248>

I am error in the 2nd line of the code here, I have a column user_id in the Estate table. What am I doing wrong here ?

myestate = Estate.where(:Mgmt => current_user.Company)
@managements = User.where(:id => myestate.user_id)

Upvotes: 3

Views: 1360

Answers (1)

Gavin Miller
Gavin Miller

Reputation: 43815

where is returning an ActiveRecord::Relation object. Because where(:mgmt => current_user.company) could return 0, 1, or Many records, you have to tell the query what you'd like from it.

Try:

myestate = Estate.where(:Mgmt => current_user.Company).first
@managements = User.where(:id => myestate.user_id)

Getting familiar with AREL and how it works is highly recommended. You can find great info on the github page or the Active Record Query Guide

Upvotes: 6

Related Questions