last day (14 days later) » 

8:04 PM
0
A: Active Record has many through, create and delete symmetry

IcemanIt's better to do it like this I feel def self.join(user, group) user.groups << group end The << is used to add an item to an array def self.kick(user, group) Membership.where(user_id: user.id, group_id: group.id).delete end

 
Do you mind elaborating on your answer? I've only seen the << used for inheritance
 
@Carpetfizz Certainly, << is used to add an item to an array, it's just a nice syntactic sugar. You might just as well do user.groups += group
 
Thanks for the update. The join function you proposed seems to work. I'm confused as to how the user is added to the group without explicitly stating. Also, the kick method didn't work (u.groups and g.users) still shows the corresponding User and Group
 
It is done by adding another group to his group array, and it's updated automatically in the db.
kick should work, it deletes the relationship between the group and the user
You can try delete! or destroy! to see if it throws an error.
 
yes, I can see how the group is added to the user, but I can't see how user is added to group. Regarding kick, yes the Membership is deleted but u.groups and g.users still shows Groups and Users respectively
Hey, thanks for taking the time!
 
8:05 PM
The user is added to a group because you explicitly use user.groups << group, that is you get the groups the user is already part of and add another group to that array.
 
Ah okay thanks, so the db knows to do something like group.users << user
 
You also have to make sure the Memberships table in the db only accepts one pair of group_id and user_id, make them a primary key or a unique key
 
How do I ensure this?
 
No, it's a Rails thing, Rails does many things for you, one of which is adding a new row to the Membership table, because you have set up your relationships in your models.
Create a unique key in the db
 
Ah okay cool trick, thank you!
I think I already have that set up

`class CreateMemberships < ActiveRecord::Migration[5.0]
def change
create_table :memberships do |t|
t.references :group, foreign_key: true
t.references :user, foreign_key: true

t.timestamps
end
end
end`
 
8:08 PM
No, that's not enough sadly
in postgres it's
create unique index membership_user_group_index on memberships(user_id, group_id)
It makes sure you're not left with the same user assigned to the same group twice
 
I think I see, I will have to figure out how to do this with SQLite. I'm still not understanding why such a thing is necessary though?
I've even tried manually deleting the membership from both the user and group objects but they still persisted
 
You don't get an error?
 
Error when doing what?
 
On a side note, I would recommend developing in Postgres from the get go, it's much easier in the long run
Deleting the membership or groups
 
Okay, I will consider doing that (just started learning rails)
Let me show you what I tried doing
Oh never mind, I do get an error
 
8:14 PM
Ahh, I thought you might be
 
What makes sense to me is just doing user.memberships.where(group: g).delete and group.memberships.where(user: g).delete
That doesn't give me an error but doesn't solve the problem either
 
That should work I believe, it must be something else, either in your db or your models that is blocking this from working
 
Would something like this do the trick?
okay, I think I'm due for some more debugging then
 
Yeah, that's the thing!
I'm just used to doing it on the db, but this is much cleaner
 
Cool, I will give it a shot! So once I do this, which method of deleting should I use, the one in your answer?
 
8:19 PM
Doesn't really matter, I just always do it the way I wrote in the answer. Find it easier to understand
Just a personal preference
 
I'm giving it a shot right now
wrong number of arguments (given 0, expected 1) for the delete method?
Looks right to me
 
What statement did you run?
 
Membership.where(user_id: user.id, group_id: group.id).delete
 
ahh sorry, should be
Membership.where(user_id: user.id, group_id: group.id).first.delete
 
oh right, testing now
 
8:25 PM
or
Membership.where(user_id: user.id, group_id: group.id).delete_all
That is what I had in mind
 
Okay so with the first one, u.groups still shows groups but g.users doesn't have any as expected
I will try the delete_all
Still the same issue
 
don't know why that is, everything seems setup correctly in the models
If you're just playing around, sometimes is just better to start from scratch, with a fresh db I mean
I'm sorry I'm not of more help
 
Right, I'll try that once more and let you know. Otherwise I will just debug on my own
No you've been a huge help, can't thank you enough
Ah yeah, still the same issue
I will play around with this more, thank you for your time and help!
 
I'm sure the error is just some minor detail that you have wrong. Are you following some tutorial or trying on your own?
 
Mostly trying on my own, I used this tutorial for reference. But it doesn't go over deletions
 
8:37 PM
Ok, good luck and don't give up, Rails can be frustrating at first. (And give postgres a try)
 
Thanks! I'm loving it so far (coming from Node/MongoDB). Will definitely give Postgres a try
Have a great day/night !
 

last day (14 days later) »