Reputation: 3489
I have a Group
and a User
which are related through a has_many :through relation. My :through table is called Groupuser
. This table has a group_id, user_id and a status. On creation of my Group i'd let to set the status of the current_user being added to 1(where default is 0).
Setting the current_user in my group is no problem at all and works perfectly fine; Its status I can't figure out though. This is what I have now:
def create
@group = Group.new(group_params)
respond_to do |format|
if @group.save
@group.users << current_user
@group.groupusers.status << 1
format.html { redirect_to @group, notice: 'done' }
format.json { render action: 'show', status: :created, location: @group }
else
format.html { render action: 'new' }
format.json { render json: @group.errors, status: :unprocessable_entity }
end
end
end
Above code throws the error: undefined method
status' for #`
My schema for Groupuser looks like this: (I've tried without ID aswell, to no avail)
create_table "groupusers", force: true do |t|
t.integer "group_id"
t.integer "user_id"
t.integer "status", default: 0
end
Upvotes: 0
Views: 66
Reputation: 15089
The @group.groupusers
is an array, activerecord relation. You can't set an attribute like that, you need to set it one by one or before you append it to the group.
@group.groupusers.each do |gu|
gu.status = 1
gu.save
end
or
groupuser = GroupUser.new user_id: current_user.id, status: 1
@group.groupusers << groupuser
Upvotes: 1