0

I'm wondering if you can help me determine the best associations and maybe new models to use for a feature I'm trying to add to an existing app. The app lets users ask questions that are answered by other users. When posting a question, a user indicates which Province it belongs to (because the questions are location specific) and the user also selects one or more categories for the question. Thus, after the questions created, I'm able to get category (choosing one category) and province information like this

     def show      
       @question = Question.find(params[:id])
       puts @question.categories.first.id      #id = 2 for Tourism, for example
       puts @question.province_id     #id = 6 for Ontario, for example

     end

What I'd like to do is let some users (who answer questions) become featured users whose profiles will be displayed down the side of the views/question/show page. So in the question show action, according to the above example, I'd to query for the featured users for Tourism category in the province of Ontario.

Considering that there will hopefully be thousands of users of the app, but only a small pool of featured users, I don't think it's best to store that information on the user model, but rather maybe create a featured_user model that connects to their main user profile, a province (for which they are an expert) and the category (for which they are an expert), but they'd only be pulled up when they match both 'province and category' at the same time, so a featured_user whose category is "Tourism" and province is "Ontario" will only be pulled up if the question is in both category "Tourism" and province "Ontario."

If I create a featured_user model, what columns and associations should I create with these other models to make it work efficiently, and/or what should I add to existing models? I'm not sure if I should be trying to use some has_and_belongs_to_many or has_many :through or simple foreign keys. My instinct is to make it more complicated than it probably should be.

Right now

Province.rb
has_many :questions

Question.rb 
belongs_to :province
has_many :categorizations
has_many :categories, through: :categorizations

Category.rb

has_many :categorizations
has_many :questions, through: :categorizations

Categorization

belongs_to :question, touch: true
belongs_to :category, touch: true

User.rb
has_many :questions #i.e. questions they've asked
has_many :answers  #i.e. questions they've answered
4

1 回答 1

2

I think the featured user model is the best solution. This encapsulate all featured user logic in one place and you can implement it without adding attribute columns to other models that (as you say) only would be used for a very small number of users. Here is how i would implement this:

# CreateFeaturedUsers (migration)
create_table :featured_users do |t|
  t.references :user
  t.references :province
  t.references :category
end

# FeaturedUser (model)
belongs_to :user
belongs_to :province
belongs_to :category

# User (model)
has_many :featured_users

# Question (model)
def featured_users
  User.joins(:featured_users).where(featured_users: {province: this.province, category: this.category})
end

However i would properly name it something else then FeaturedUser as the model doesn't represent a user but only the relation to a user. Maybe Featuring would be a better name.

于 2013-06-15T15:22:45.310 回答