开发者

Joining a one-to-many association with a many-to-many association in Rails 3

开发者 https://www.devze.com 2023-01-04 07:52 出处:网络
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 tryin

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号