I have a many-to-many association between a User class and a Table class. Additionally, i have a one-to-many association between the User and the Table (one User ultimately owns the table). I am trying to access all of the tables which the user may access (essentially joining both associations).
Additionally, it would be nice to do this this with named_scope (now scope)
Here's what I have so far:
class User < ActiveRecord::Base
acts_as_authentic
attr_accessible :email, :password, :password_confirmation
has_many :feedbacks
has_many :tables
has_m开发者_高级运维any :user_table_permissions
has_many :editableTables, :class_name => "Table", :through => :user_table_permissions
def allTables
editableTables.merge(tables)
end
end
I have posted about a very similar problem recently. (http://stackoverflow.com/questions/4085972/ruby-on-rails-combine-results-from-multiple-has-many-or-has-many-through-associa) My two associations have the additional handicap that one is polymorphic, but essentially, we both want to combine two association results using one finder, and have the result set available for further processing using finders, named scopes or other AR features.
Currently, it seems not possible without losing exactly this ActiveRecord functionality (either using 'def' as you above or by using :finder_sql and :counter_sql in the association setup). If you come up with a way of doing this, I would really appreciate if you could tell me.
Thanks!
Jens
精彩评论