Spidy
Spidy

Reputation: 40002

Rails model relationships with custom names

EDIT

Simple relationship but I'm having an issue getting it to work. There is a User. User's have many Bounties. User's have many BountyVotes through Bounty. Bounties have BountyVotes. For readability, I call BountyVotes -> Votes in the Bounty class. I get a Name Error: uninitialized constant User::bounty_vote when trying to access bounty_interests from the User model.

A user can create a Bounty. Other user's can vote on the Bounty.

//User class
class User < ActiveRecord::Base
  has_many :bounties
  has_many :bounty_interests, :through => :bounties, :source => :votes
end

//Bounty class
class Bounty < ActiveRecord::Base
  belongs_to :user
  has_many :votes, :class_name => :bounty_vote
end

//Bounty Vote class
class BountyVote < ActiveRecord::Base
  belongs_to :bounty
end

Upvotes: 0

Views: 1101

Answers (2)

Spidy
Spidy

Reputation: 40002

Had to change two things. First, thanks to shakerlxxv I needed to change my through to be plural.

has_many :bounty_interests, :through => :bounties, :source => :votes

Than I also had to change the way I referenced my class name.

has_many :votes, :class_name => 'BountyVote'

Upvotes: 2

shakerlxxv
shakerlxxv

Reputation: 437

Your :through clause needs to reference the plural form:

has_many :bounty_interests, :through => :bounties, :source => :votes

Upvotes: 1

Related Questions