T&P
To run server
rails s
To start and run postgres database via docker:
docker start postgres
To drop, create and migrate database
rake db:drop:all db:create db:migrate
Notes on Fetch: So the problem is that if i do my_hash[:foo] when my_hash has no :foo key it returns nil And then if I chain on another [:bar] I get an error because nil doesn't implement a [] method So what if I used a method that returned something that did have that method on it instead, as a default value The thing I want to index into is a hash, so I could use an empty hash my_hash.fetch(:foo, {})[:bar] for example .fetch is like [], but it takes a second arg, which is a default value to use
attr_reader :current_user
def current_user @current_user end
def my_attr_reader(field_name) define_method(field_name) do get_instance_variable(field_name) end end
once created hte jsonb preferences using user = User.create({email: "[email protected]", password: "password", preferences: {tea: "black", milk: true, sugar: 2}})
you can then add show preferences with user.preferences and add them using using user.preferences["smug"] = "very, yes" for example and as its type is a hash you can also do user.preferences.merge("coffee" => false, "sizes" => [1,2,3]) for example but you must use user.save!
SYNTAX NOTE so with ruby, if a symbol ( :symbol ) is ALSO a key, you can move the colon to the end and forget about the hashrocket ( => ) so :symbol => :value is the same as symbol: :value
and the second way is the correct way. Now.
NB A symbol is sort of like a string that is used for naming things. Like an atom in elixir or clojure
Using the groovy to_proc oprator with map: group.users.map(&:name)
Getting an array of keys from database group.users.pluck(:name)
REPL use rails c for a rails console but use bundle exec rails c for an amazing console tied to your app and database wooohooo
ADD A FOREIGN KEY constraint MOFO because it isnt automatic for joined tables which means that you could say have a table that has many x and y which belongs to x and then delete x but still have this random y floating around so in our case we could have a user Alice being part of group ihop and there would be a group_membership called alicetoihop but if we delete user Alice we still have the bloody alicetoihop floating around being annoying. Foreign key constraints make this NOT happen, which is good. They make sure you have referential integrity
INVITES so there needs to be an invite method for the user? or the group? basically if a user wants to add someone to the group, from the group/invites route maybe? they send a request which is the name of the person to invite. I think it should be a groups/invite post request. I also think that on the invites page you should be able to search for the user to invite so maybe i need to do search first?
There also needs to be a show user invites route where a user can see what groups they have been invited to, and a user/invite post route where they can send or reject the invite.
So I need an invites route and an invites controller and also, an invites colum in the database. A user can have many invites and in the invites table, I need to have a column called accepted? And this is the colum that links to the groups? Or just in fact, the invites is a join table between user and groups but has an 'accepted: true or false column? Then when I send the group membership invites I only show the ones with accepted? That way I can use the group membership table. And that way it can say 'is already a member' if they are already in it...?
This will mean that the group membership model must have a validation for :accepted column. like it has to exist. And the type is :boolean.
So:
When a user creates a group they automatically have accepted: true. When a user adds another user to a group, htey automatically have accepted: false. The route for this should be /groups/:name/invite and it should be a post becasue you are creating the invite and that user doesn't automatically have a coumn in the group membership table. It should be in the groups/:name/update route though? This is confusing.
A user can go to the route user/invites show and see their invites. from here they can do a patch request to accept the invite, going from false to true. (or false)
What is the problem exactly? I want search_users (a variable) to pass it's resulting array to the SearchView module and the user_list method
What I think should happen? search users returns an array (yes)
user_list performs a map on the array (no - it says map is an undefined method)
what happens using pry I can do search_users.map do |user| {name: user.name} end
and this works but user_list(search_users) fails...
and also user_list(new_user3) fails the same way
changed user_list method so that search_users becomes search_users.to_a.map
now user_list works but search_results fails ...