Richlewis
Richlewis

Reputation: 15384

Selecting favourites from DB rails 3

I have a small app which has Users, Recipes, Ingredients and preparation models

A user has many recipes, recipes belong to user and ingredients/preparation belongs to recipes.

Now a user can view all recipes but I would like the option to add the particular recipe to a favourites list. Would I need to set a new DB to hold this and then link by associations or could I add a column to the recipe model called fav for example?

Im looking for the best practice here or if someone has done this before and can offer any advice that would be appreciated

Upvotes: 0

Views: 40

Answers (2)

Jason Noble
Jason Noble

Reputation: 3766

I would add a boolean column favorited to your UsersRecipe table.

Then you can add a scope for this in your User model:

has_many :favorite_recipes, :through => :user_recipes, :class => Recipe, :conditions => ['user_recipes.favorited = ?',true]

Upvotes: 1

Thanh
Thanh

Reputation: 8604

I think you can set has_may association like this:

class User < ActiveRecord::Base

  has_many :favorite_recipes, foreign_key: "user_id", class_name: "Recipe", dependent: :destroy
end

Upvotes: 1

Related Questions