1

I try to run the test but it fails as below.

[user1 project]$  rake test
DEPRECATION WARNING: String based terminators are deprecated, please use a lambda. (called from included at /home/.gem/ruby/2.1.3/bundler/gems/authlogic-09163c7d2a9b/lib/authlogic/session/callbacks.rb:66)
DEPRECATION WARNING: String based terminators are deprecated, please use a lambda. (called from included at /home/.gem/ruby/2.1.3/bundler/gems/authlogic-09163c7d2a9b/lib/authlogic/session/callbacks.rb:67)
Started

ERROR["test_login_and_access_bookmarks/managing_bookmarks", BookmarksTest, 0.120519093]
 test_login_and_access_bookmarks/managing_bookmarks#BookmarksTest (0.12s)
ActiveRecord::StatementInvalid:         ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'user_id' in 'field list': INSERT INTO `roles` (`id`, `user_id`, `created_at`, `updated_at`) VALUES (4, 2, '2015-03-15 17:54:24', '2015-03-15 17:54:24')


  1/1: [=========================================================================================================] 100% Time: 00:00:00, Time: 00:00:00

Finished in 0.12271s
1 tests, 0 assertions, 0 failures, 1 errors, 0 skips
[user1 project]$  

I have two tables users and roles and two fixtures named users.yml and roles.yml.

Content of users.yml :

# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html


user2:
  name: user2
  id: 2

Contents of roles.yml:

user2_1:
  id: 4
  user_id: 2

What could be the problem here? I am trying to link the users table with roles establishing a relationship such that - user2 from users table has role id 4 in roles table. Would appreciate any help/advice. Thanks!

I checked some stackoverflow posts like this- ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column but did not find a working answer.

Structure of roles table:

mysql> describe roles
    -> ;
+-----------------+--------------
| Field           | Type         
+-----------------+--------------
| id              | int(11)      
| name            | varchar(255) 
| parent_id       | int(11)      
| description     | varchar(255) 
| default_page_id | int(11)      
| cache           | text         
| created_at      | datetime     
| updated_at      | datetime     
+-----------------+--------------
Community
  • 1
  • 1
Zack
  • 2,078
  • 10
  • 33
  • 58

1 Answers1

2

There is no user_id field in the roles table; most likely you are trying to specify parent_id here.

Best way to implement the association between users and roles in fixtures would be as follows:

# users.yml
user2:
  name: User 2

# roles.yml
role2:
  name: Second role
  parent: user2 (User)

Note: Setting parent: user2 (User) in roles.yml makes the role2 object use user2 as the parent, and since the object types are different (parent vs user), the (User) string should be included to denote the polymorphic relationship.

Please do go over the http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html page to get familiar with fixtures.

Prakash Murthy
  • 12,923
  • 3
  • 46
  • 74