导轨,两款车型之间建立的最爱 [英] Rails, setting up favorites between two models

查看:118
本文介绍了导轨,两款车型之间建立的最爱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着从步骤这里设置一个最爱的关系用户模式和公司模式之间。具体地,用户可以有许多喜爱的公司。还有的用户被束缚在一个公司上创建功能。

I followed the steps from here to setup a favorites relationship between a user model and a company model. Specifically, a user can have many favorite companies. There is also a functionality of a user being tied to a company on creation.

因此​​,这里是我目前的模型/路由/ DB模式

So here are my current models/routes/db schema

模式

class FavoriteCompany < ActiveRecord::Base
    belongs_to :company
    belongs_to :user
...

class User < ActiveRecord::Base

  has_many :companies

  has_many :favorite_companies
  has_many :favorites, through: :favorite_companies
...

class Company < ActiveRecord::Base

    belongs_to :user

    has_many :favorite_companies
    has_many :favorited_by, through: :favorite_companies, source: :user

路线(可能不适用)

resources :companies do
  put :favorite, on: :member
end

DB模式

ActiveRecord::Schema.define(version: 20140429010557) do

  create_table "companies", force: true do |t|
    t.string   "name"
    t.string   "address"
    t.string   "website"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.float    "latitude"
    t.float    "longitude"
    t.text     "popup",         limit: 255
    t.text     "description",   limit: 255
    t.string   "primary_field"
    t.string   "size"
    t.integer  "user_id"
  end

  create_table "favorite_companies", force: true do |t|
    t.integer  "company_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "email"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "password_digest"
    t.string   "remember_token"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["remember_token"], name: "index_users_on_remember_token"

end

现在,当我尝试轨控制台中访问命令 user.favorites (这里的用户是已经创建的用户)。我碰到下面的错误。

Now when I try to access the command user.favorites (Where user is an already created user) within the rails console. I get the following error.

ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :favorite or :favorites in model FavoriteCompany. 
Try 'has_many :favorites, :through => :favorite_companies, :source => <name>'. Is it one of :company or :user?

建议的修复似乎并不喜欢在这种情况下做正确的事情,我不能为我的生活出了什么错误。

The suggested fix doesn't seem like the right thing to do in this case, and I can't for the life of me figure out what is going wrong.

推荐答案

在你的用户模式,这条线从改变:

In your User model, change this line from:

has_many :favorites, through: :favorite_companies

这样:

has_many :favorites, through: :favorite_companies, source: :company

您在使用你的公司模型的正确语法:favorited_by

You used the correct syntax in your Company model for :favorited_by.

这篇关于导轨,两款车型之间建立的最爱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆