I have a table which maps users to photo album as hosts:
> Album table | => | Hosts | <=> | Users |
Sometimes I need to query if a user is a host of an album:
if album.hosts.include? user
Which I cannot do because "hosts" is an array of Host models -- not users. I was wondering which solution be faster, or perform better.
This:
has_many :host_users, :through => :hosts, :source => :user
Or:
def host_users
# Create table as instance variable if !@hostUsersTbl hosts.each do |h| @hostUsersTbl[h.user.id] = h end end
@hostUsersTbl end
The first solution would make an extra call to the DB, but the second is manually creating the list. I guess I could also do this instead:
has_and_belongs_to_many :hosts, :class_name => "User", :join_table => "hosts"
But then I wouldn't have the Host model array to manipulate (i.e. create, delete), right? Please advise.
Thanks, Jeremy