This is really bugging me. How can I get a second association to work
between 2 tables.
I have two models, User and Article. A user has many articles, and an
article belongs to one user. Then I have the “current article”, which
is a the article that the user is currently viewing/editing. I want to
track/save this value, so they can come back to the same article if they
log out and back in again.
The database tables are set up fairly basically, with user_id as a
foreign key in the articles table, and current_article_id as a foreign
key in the users table.
The model associations are defined as follows:
class User < ActiveRecord::Base
has_many :articles, :order => “name”, :dependent => :delete_all
belongs_to :current_article, :class_name => “Article”, :foreign_key =>
“current_article_id”
end
class Article < ActiveRecord::Base
class Search < ActiveRecord::Base
belongs_to :user
has_one :current_for_user, :class_name =>“User”, :foreign_key =>
“current_article_id”
end
I simply cannot make the “current_article” association work reliably.
Ideally, I want to be able to set the current article manually (when the
user selects an existing article to edit or view), and also to set the
current_article automatically (when a new article is created).
Has anyone managed to get something like this working? If so, PLEASE
share the details!