missing attribute when using find_by_sql with RSPEC

898 views Asked by At

I use a find_by_sql statement which works fine in development. When it comes to the test environment using spec, it outputs the error:

ActiveModel::MissingAttributeError: missing attribute: registered

I have run db:test:purge and db:test:prepare to clean up the test database. But the error still occurs. In development and test I use PostgreSQL.

The problematic statement is the following:

def registered_friends
  User.find_by_sql(
  ["select users.id, users.first_name from users inner join friendships
    on users.id=friendships.friend_id
    where friendships.user_id = ?
    and users.registered = true",self.id])
end  
1

There are 1 answers

0
Ralph Allen On

Thanks Sebastien,

I found the problem. In my model I initialize the field "registered" to set it by default to "false". As the SQL-query didn't return the field "registered", it broke at this point. I found it after it also broke in development stage.