活动记录关联错误

时间:2014-06-09 14:55:16

标签: ruby-on-rails database activerecord associations favorites

我正在创建一个包含食谱数据库的Web应用程序。我正在努力实现一项功能,允许用户将食谱添加到他们的收藏列表中,并知道这涉及Active Record关联。以下是我的尝试。不幸的是,我收到了以下错误消息。

Could not find the source association(s) :favorite or :favorites in model FavoriteRecipe. Try 'has_many :favorites, :through => :favorite_recipes, :source => <name>'. Is it one of :recipe or :user?

我在ActiveRecord Associations下搜索了RubyonRails指南并尝试了这段代码,但它引发了上述错误。

这是我的食谱模型,Recipe.rb

class Recipe < ActiveRecord::Base
  belongs_to :user
  has_many :favorite_recipes
  has_many :favorited_by, through: :favorite_recipes, source: :user
end

这是我的用户模型,User.rb

class User < ActiveRecord::Base
  has_many :recipes
  has_many :favorite_recipes
  has_many :favorites, through: :favorite_recipes
end

这是我最喜欢的食谱模型,favorite_recipe.rb

class FavoriteRecipe < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :favorite
  belongs_to :user
end

这是我最喜欢的食谱迁移文件

class CreateFavoriteRecipes < ActiveRecord::Migration
  def change
    create_table :favorite_recipes do |t|
      t.string :recipe_id
      t.string :integer
      t.string :user_id
      t.string :integer

    t.timestamps
    end
  end
end

在Rails控制台中玩,我尝试了以下

FavoriteRecipe.table_name
=> "favorite_recipes"

FavoriteRecipe.attribute_names
=>  ["id", "recipe_id", "integer", "user_id", "created_at", "updated_at"] 

FavoriteRecipe.primary_key
=> "id"

FavoriteRecipe.column_names
=> ["id", "recipe_id", "integer", "user_id", "created_at", "updated_at"]

FavoriteRecipe.connected?
=> "true"

这是我的routes.rb文件

get "users/new"
  resources :recipes do
    put :favorite, on: :member
  end

  resources :users
  resources :sessions, only: [:new, :create, :destroy]

get 'static_pages/about'
get 'static_pages/contact'
get 'static_pages/help'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'

运行rake路线时,这是我的输出

users_new_path           GET     /users/new(.:format)            users#new
favorite_recipe_path       PUT     /recipes/:id/favorite(.:format)   recipes#favorite
recipes_path               GET     /recipes(.:format)                recipes#index
                     POST    /recipes(.:format)                recipes#create
new_recipe_path            GET     /recipes/new(.:format)            recipes#new
edit_recipe_path           GET     /recipes/:id/edit(.:format)       recipes#edit
recipe_path                GET     /recipes/:id(.:format)            recipes#show
                     PATCH   /recipes/:id(.:format)            recipes#update
                     PUT     /recipes/:id(.:format)            recipes#update
                     DELETE  /recipes/:id(.:format)            recipes#destroy
users_path               GET     /users(.:format)                users#index
                     POST    /users(.:format)                users#create
new_user_path            GET     /users/new(.:format)            users#new
edit_user_path           GET     /users/:id/edit(.:format)       users#edit
user_path                GET     /users/:id(.:format)            users#show
                     PATCH   /users/:id(.:format)            users#update
                     PUT     /users/:id(.:format)            users#update
                     DELETE  /users/:id(.:format)            users#destroy
sessions_path            POST    /sessions(.:format)             sessions#create
new_session_path         GET     /sessions/new(.:format)         sessions#new
session_path             DELETE  /sessions/:id(.:format)         sessions#destroy
static_pages_about_path  GET     /static_pages/about(.:format)   static_pages#about
static_pages_contact_pathGET     /static_pages/contact(.:format  static_pages#contact
static_pages_help_path   GET     /static_pages/help(.:format)    static_pages#help
signup_path              GET     /signup(.:format)               users#new
signin_path              GET     /signin(.:format)               sessions#new
signout_path             DELETE  /signout(.:format)              sessions#destroy

当用户点击&#34;显示&#34;时,它会显示特定的食谱,并有一个链接,您可以将其添加到收藏列表中。这是我的食谱/ show.html.erb文件

<h3>Recipe</h3>
 <ul id = 'view'>
  <li><strong>Title:</strong> <%= @recipe.title %></li>
  <li><strong>Console:</strong> <%= @recipe.console %></li>
  <li><strong>Genre:</strong> <%= @recipe.genre %></li>
  <li><strong>Release Date:</strong> <%= @recipe.released_on %></li>
</ul>

<p>
<%= link_to 'Add to Favorites', favorite_recipe_path(@recipe, type: 'favorite'), method: :put, id: 'favorites' %>
</p>

<p>
  <%= link_to 'Back', recipes_path, id: 'back' %>
</p>

我在recipes_controller.rb文件中定义了以下方法

def favorite
  type = params[:type]
  if type == "favorite"
    current_user.favorites << @recipe
    redirect_to :back, notice: "#{@recipe.name} successfully added to your favorites!"
  elsif type == "unfavorite"
    current_user.favorites.delete(@recipe)
    redirect_to :back, notice: "#{recipe.name} removed from your favorites"
  else
    redirect_to :back, notice: 'No change'
  end
end

道歉。我是Ruby的初学者,但仍然不太了解Active Record Associations。但是,我们非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我不确定这是否反映了您的实际迁移,但这是完全错误的:

class CreateFavoriteGames < ActiveRecord::Migration
  def change
    create_table :favorite_games do |t|
      t.string :game_id
      t.string :integer
      t.string :user_id
      t.string :integer

    t.timestamps
    end
  end
end

应该是:

class CreateFavoriteGames < ActiveRecord::Migration
  def change
    create_table :favorite_games do |t|
      t.integer :game_id
      t.integer :user_id
      t.integer :favorite_id

      t.timestamps
    end

    add_index :favorite_games, :game_id
    add_index :favorite_games, :user_id
    add_index :favorite_games, :favorite_id
    add_index :favorite_games, [:game_id, :user_id, :favorite_id], unique: true
  end
end