Reputation: 1045
I created a model using the Rails generate
command:
rails generate model Group users:string mo@x1[35%]
invoke active_record
create db/migrate/20151130131319_create_groups.rb
create app/models/group.rb
invoke test_unit
create test/models/group_test.rb
create test/fixtures/groups.yml
Now in the rails console, when I try to create an object, it gives me an undefined method
error.
2.0.0-p647 :032 > Google = Group.create(users: "Google")
NoMethodError: undefined method `each' for "Google":String
Can anyone shed any light on this?
This is the .rb
file of Group
:
class Group < ActiveRecord::Base
end
Upvotes: 1
Views: 1216
Reputation: 2996
Does your group model have a has_many association named :users?
It sounds to me like you have a naming conflict between your db column called "users" and the has_many association with your User model, also called "users".
If so, your create() is trying to set up the associated user records with a string, when it is expecting an array of users.
Best workaround here is to rename your database column in your groups table from "users" to something that won't clash.
Upvotes: 1
Reputation: 5812
Check the migration file. Maybe there's something wrong with the code block.
Upvotes: 0