matthewvb
matthewvb

Reputation: 942

Rails correct association with non-standard foreign key

I have two tables:

Languages: id, name, abbreviation
Post: id, title, language

Post.language is connected to Language.abbreviation.

How do I set up the Models so I can correctly call:

@post.language.name 

and receive the name of the language it's connected to?

What I was starting with was:

Post> 
  belongs_to :language

Language>
  has_many :posts, :foreign_key => "abbreviation"

But that doesn't seem to be connecting properly.

Thanks for the help.

Upvotes: 0

Views: 133

Answers (1)

Solomon
Solomon

Reputation: 7023

I would store the language_id in the Post model, instead of the abbreviation. You can connect the abbreviations in forms to show the abbreviation, but store the language_id in the database.

Upvotes: 1

Related Questions