associations - Rails, setting up favorites between two models -
I followed the steps to establish a preferred relationship between a user model and a company model. Specifically, the user may have many favorite companies
so my current model / route / DB schema
model
class favoritecompany & lt; ActiveRecord :: Base is_to: company belongs_to: user ... class user & lt; ActiveRecord :: Base is has_many: Companies are_im: favorites_company has_many: favorites, via: :: favorites_company ... class company & lt; ActiveRecord :: Base is_many: user_many: Favorites_company has_many: favorited_by, via :: Favorites_company, source :: user
route (may not apply) )
ActiveRecord :: Schema.define (version: 20140429010557) Create_full "companies", force: true. 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", border : 255 T. Text "Details", range: 255t. 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: Now when I try to use the user.favorites command, "add_index_email" / code> (where the user is already a user created) within the Rail Console I get the following error. ActiveRecord :: HasManyThroughSourceAssociationNotFoundError: Source Association could not be found: Favorites or Favorites: Favorites in Favorites Favorite Computers Try 'has_many: favorites, via: = & gt; : Favorite_company ,: Source = & gt; & Lt; Name & gt; '. Is this a company: or user? Recommended fixes do not look like doing the right thing in this case, and I can not understand what is wrong for my life. In your user
model, replace this line with:
Hdma: Favorites, via :: Favorites_companies
To do this:
is_im: favorites, through :: Favorite_company, Source :: Company
You used the right syntax for your company
model in : favorited_by
.
Comments
Post a Comment