Could not find the source association(s) :followed_id in model relationship in Rails 3.2
Found the error:
In my user model I had to change
has_many :followed_users, through: :relationships, source: "followed_id"
to
has_many :followed_users, through: :relationships, source: :followed
Seems to be a typo in Hartl's tutorial Listing 11.10 http://ruby.railstutorial.org/book/ruby-on-rails-tutorial#code:has_many_following_through_relationships, since that's where I got the "source: "followed_id"" code from.
I got the fixed code from Hartl's github "sample app".
Could not find the source association(s) :character in model Relation
OK, I think the problem is with naming. It's not a good idea naming association and column with same name. If it's possible start with renaming columns on relation:
owner => owner_id
character => character_id
Alternatively You can rename associations:
owner => user_owner
character => user_character
Another thing that I see is Your separate characters table. You seem like want to reference characters table from Your Relation model (correct me if I am wrong), but never do it.
belongs_to :user_character, foreign_key: "character"
Now to Your main question. If I understood Your correctly You want to get list of characters, using friends association defined in User. In this case the line should look like this:
has_many :friends, -> { where reltype: "friend" }, through: :relations, source: "user_character", class_name: "Character"
UPDATED
Author explained that he really wants to reference User model through friends
association.
# relation.rb
belongs_to :user_character, foreign_key: "character", class_name: "User"
# user.rb
has_many :friends, -> { where reltype: "friend" }, through: :relations, source: "user_character"
UPDATE2
There was a problem with association condition, in particular case You need to specify table explicitly:
has_many :friends, -> { where("relations.reltype" => "friend") }, through: :relations, source: "user_character"
Rails 4 Error: ActiveRecord::HasManyThroughSourceAssociationNotFoundError
Here is my suggestion:
user.rb
userfavorites
- will list all user you have favorited
has_many :favorite_relationships, class_name: "FavoriteUser", foreign_key: "c_user_id"
has_many :userfavorites, through: :favorite_relationships, source: :user
userfavorited_by
- will list all user who favorited you
has_many :favorited_relationships, class_name: "FavoriteUser", foreign_key: "user_id"
has_many :userfavorited_by, through: :favorited_relationships, source: :c_user
FavoriteUser
edited:
class FavoriteUser < ActiveRecord::Base
belongs_to :c_user, class_name: "User"
belongs_to :user, class_name: "User"
end
After you make changes, make sure to test
first in your console
that the relationships works. then make appropriate change in your view, controller etc...
ActiveRecord::HasManyThroughAssociationNotFoundError
Add this to User
class
has_many :collected_posts, through: :collections, source: :posts
Rails Tutorial 11.1 - ActiveRecord::HasManyThroughSourceAssociationNotFoundError:
You should use
has_many :followed_users, through: :relationships, source: :followed
The is because source
wants the name of the association that the records are loaded from (not the foreign key)
Rails Model Association: Favoriting
You have
has_many :favorites, :through => :favorite_projects
But there is no favorites
association in FavoriteProject
model. Only belongs_to :project
. It should be:
has_many :favorites, :through => :favorite_projects, :source => :project
Related Topics
Is There a Gem That Normalizes and Format Us Phone Numbers in Ruby
Duplicating a Ruby Array of Strings
Activerecords Select(:Id).Collect VS. Pluck(:Id) Methods: Why Is Pure Ar "Pluck" Slower
How to Search Array Through Ransack Gem
Best Way to Handle Category/Subcategory Relationship Ruby on Rails
Is There a Pry Debug Setup That Works with Ruby 2.0
2 Gems Need Different Versions of the Same Dependency
Call Before Methods in Model on Ruby
How to Scrape Pages Which Have Lazy Loading
How to Do "Late" String Interpolation in Ruby
Ruby Selenium Webdriver Unable to Find Mozilla Geckodriver
Create a Human-Readable List with "And" Inserted Before the Last Element from a Ruby List
Getaddrinfo Error with Mechanize
Soap Client for Ruby 1.9 and Rails
Find Products Matching All Categories (Rails 3.1)
How to Make Ruby's Restclient Gem Respect Content_Type on Post