How to set foreignkey in such a situation?


#1

Table articles has column: author,body,etc
Table users has column: login_name,email,etc
articles belongs_to user
users has many articles

foreign_key author references users(login_name)

Therefore in Article.rb:

belongs_to:article_user,
:class_name=>“User”,
:foreign_key=>“author”
and what’s going on? how the rails find the reference relationship
between articules(author) and users(login_name)?


#2

Charlie wrote:

Table articles has column: author,body,etc
Table users has column: login_name,email,etc
articles belongs_to user
users has many articles

foreign_key author references users(login_name)

Therefore in Article.rb:

belongs_to:article_user,
:class_name=>“User”,
:foreign_key=>“author”
and what’s going on? how the rails find the reference relationship
between articules(author) and users(login_name)?

You can use the :finder_sql option when to define the association, to
set what you want:

class Article < ActiveRecord::Base
belongs_to :article_user,
:class_name => “User”,
:finder_sql => “select u.* from user u, article a where u.login_name
= a.author”

I believe that’ll do what you’re looking for.

Jeff C.man