简体   繁体   中英

Ruby on Rails - create function to list users

I have a Users table and a Team table. The foreign team key is in the user table. But I want to create a function to list the users so that I can choose the users of that team. That is, I want to call this function with some select or something of the type in the Team view. What would that function look like and what would I call it in the team view? obs: although the foreign key of the team is in the user table, usually in the user view I would choose a team, but what I want to do is in the opposite view.

This is really basic stuff. Perhaps you should review the guide to understand how associations work. Here's, roughly, how it goes...

If you set your User model up like (ignoring other attributes you probably have):

# == Schema Information
#
# Table name: users
#
#  id         :bigint           not null, primary key
#  team_id    :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class User < ApplicationRecord
  belongs_to :team
end

And you set your Team model up like (ignoring other attributes you probably have):

# == Schema Information
#
# Table name: teams
#
#  id         :bigint           not null, primary key
#  name       :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class Team < ApplicationRecord
  has_many :users
end

And you get instantiate a @team variable something like:

@team = Team.find_by(id: 1)

Then you can do:

@team.users 

to get all the User s associated with the Team .

BTW, this will require that a User has a Team to be saved. If you try to save a User without a Team , it won't work. If you want to save a User without a Team , then you'll have to do:

# == Schema Information
#
# Table name: users
#
#  id         :bigint           not null, primary key
#  team_id    :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class User < ApplicationRecord
  belongs_to :team, optional: true
end

Personally, I would prefer to do @team.members (because that seems more natural than @team.users , IMO). So, I would do something more like:

# == Schema Information
#
# Table name: teams
#
#  id         :bigint           not null, primary key
#  name       :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

class User < ApplicationRecord
  has_many :members, class_name: 'User'
end

Then you can do:

@team.members

and, again, get all the User s associated with the Team .

BTW, in Ruby, we write methods , not functions .

Make sure User.all is defined in its class and make a function using something like this.

User.all.select{|user| user.team=="Hornets" }

This is A helpful link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM