jwood / tenacity

A database client independent way of managing relationships between models backed by different databases.
MIT License
118 stars 17 forks source link

How do I chain the conditions on an association pointing to a Mongoid model? #39

Closed kandadaboggu closed 12 years ago

kandadaboggu commented 12 years ago

How do I chain the conditions on an association pointing to a Mongoid model?

class User < ActiveRecord::Base
  include Tenacity
  t_has_many :posts
end

class Post
  include Mongoid::Document
  include Tenacity
  t_belongs_to :user
end

When I try to apply a conditions on the has_many association tenacity throws error( undefined method 'where' for []:Array)

current_user.posts.where(:name => "foo") # throws an exception
jwood commented 12 years ago

Sorry, but tenacity does not support conditions on the associations.

However, you may be able to use User#post_ids to limit the query to only posts that belong to the user, and then add your additional query parameter. Perhaps something like:

Post.where(:id => current_user.post_ids, :name => "foo")
kandadaboggu commented 12 years ago

Sorry, but tenacity does not support conditions on the associations.

I wanted to confirmed that.

As of now I am directly querying the Post model as follows:

Post.where(:user_id => current_user.id, :name => "foo")

PS: Is Mongoid 3 support in the roadmap?

jwood commented 12 years ago

I'm not actively working on tenacity at the moment, just fixing bugs and merging pull requests. So, there are no immediate plans to support Mongoid 3. However, if somebody were to add support for it, I would happily merge it in.