We've moved discussions to Discord

Current_user or team

Andrew Hodson
A user has listings but they are on a team. Do I use current_user.listings or current_user.listings? 

Thanks
I love this!
Chris Oliver
Hey Andrew,

You can add an association to your User model that's a has_many through.

Something like this: has_many :listings, through: :teams
Andrew Hodson
sorry, I just want to make sure I'm clear. I taught myself everything over the years so it takes some time for me lol
I would then need to add the user:references and  team:references on the models correct?
Chris Oliver
You're totally fine. I'm self-taught too, so it took me forever to learn it too. :)

Teams is already added to the User model through the UserTeams concern. 👍
Andrew Hodson
So if I add both those references to the models I can use current_user.listings to get that users listings and current_team.listings to get all the listings of the team?
Chris Oliver
Just add the has_many :listings, through: :teams to the User model and you should be set to use current_user.listings
Andrew Hodson
And that will only pull that users listings not the whole team correct? 
Chris Oliver
That would pull all the listings from the teams they are associated with.

To filter down to theirs out of the associated team listings, I'm assuming you have a belongs_to :user on the Listing model? If so, you can do:

has_many :listings, ->(user) { where(user_id: user.id) }, through: :teams
Andrew Hodson
Sweet! thank you. One more question. Is it possible to setup to have a paywall at creating a team?
Chris Oliver
Sure could. Teams already have billing separate for each one, so you could just add payments to the new team form and update the controller to process payment along with create.

The easiest option might be to let them create a team, but don't let them do anything without subscribing/paying.
Andrew Hodson
That's what I was thinking you can create the team (company account) but can't do anything until the team is active.
Andrew Hodson
The only problem is I have free accounts clients (user) and then company accounts (user with a company profile created) how can I keep the user free and able to list requests but blocking company (teams) from doing anything 
Chris Oliver
There's a "personal" team created by default, so you could check if they're logged into that one or a regular team. That way you could say allow creating a listing if it's personal, but don't let non-personal teams create them.

Just check if current_team.personal? 
Chris Oliver
Teams are getting renamed to Accounts in an upcoming update, so they will be a bit less confusing.
Andrew Hodson
Yeah I was reading about that if I keep with the current way of doing things will I be able to pull that update in ?
Chris Oliver
It shouldn't be too bad of an upgrade, but you will have to rename some things in your own code. 

If you wanted to use it today, you can switch to the multitenancy branch on git. I'd recommend doing this if you haven't gotten too far along quite yet.
Andrew Hodson
Done. Thanks!
Notifications
You’re not receiving notifications from this thread.