Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support getting criteria for has_many: through: associations #159

Open
robertjpayne opened this issue Aug 30, 2015 · 8 comments
Open

Support getting criteria for has_many: through: associations #159

robertjpayne opened this issue Aug 30, 2015 · 8 comments
Labels
enhancement Improve what already exist.

Comments

@robertjpayne
Copy link

I can't seem to find a way to get a valid criteria object for an association, I dug through the source and looked at what's given in the reflection API but it doesn't seem there is a way to get a criteria object?

It would be really great for instances to extend it or manipulate the query. In this particular case I am attempting to apply a offset and limit based on API input to a association.

@nviennot
Copy link
Collaborator

That should work, can you post some sample code?

@robertjpayne
Copy link
Author

Might be because it's a has_many through?

require 'nobrainer'
require 'excon'
require 'logger'

logger = Logger.new(STDOUT)

NoBrainer.configure do |c|
  c.app_name = 'test'
  c.environment = 'development'
  c.rethinkdb_urls = [c.default_rethinkdb_url]
  c.logger = logger
end

class User
  include NoBrainer::Document

  field :name, type: String, required: true

  has_many :followingships, class_name: 'Followship', foreign_key: :follower_id, dependent: :restrict
  has_many :followerships, class_name: 'Followship', foreign_key: :following_id, dependent: :restrict

  has_many :following, through: :followingships
  has_many :followers, through: :followerships

end

class Followship
  include NoBrainer::Document

  field :follower_id, type: String, required: true, index: true
  field :following_id, type: String, required: true, index: true

  belongs_to :follower, class: 'User'
  belongs_to :following, class: 'User'

  index :follower_id_and_following_id, [:follower_id, :following_id]

end

NoBrainer.sync_indexes

User.delete_all
Followship.delete_all

robert = User.create!(name: 'Robert')
karen = User.create!(name: 'Karen')
pablo = User.create!(name: 'Pablo')
ivan = User.create!(name: 'Ivan')
john = User.create!(name: 'John')
andrea = User.create!(name: 'Andrea')

Followship.create!(follower: robert, following: karen)
Followship.create!(follower: robert, following: pablo)
Followship.create!(follower: robert, following: john)
Followship.create!(follower: robert, following: ivan)
Followship.create!(follower: robert, following: andrea)

Followship.create!(follower: john, following: robert)
Followship.create!(follower: john, following: karen)

Followship.create!(follower: andrea, following: robert)

current_user = john
user = robert

puts robert.followers.offset(2).limit(1).to_a

@robertjpayne
Copy link
Author

Ah yea looks because it's a has_many though hmm.

@robertjpayne robertjpayne changed the title Support getting criteria for associations Support getting criteria for has_many: through: associations Aug 30, 2015
@nviennot
Copy link
Collaborator

Oh indeed. With has_many_through you don't get a criteria. I'll see how to work around with that.

@nviennot
Copy link
Collaborator

This is actually complicated to implement, so I'll punt for now.

You can do something like this:

robert.followerships.join(:follower).offset(x).limit(1).map(&:follower)

You can also use eager_load instead of join. Not sure which is faster.

@robertjpayne
Copy link
Author

@nviennot yea that is what I am doing right now, it works would just be nice if it was supported otherwise.

@nviennot
Copy link
Collaborator

I agree.

btw, how's your experience with NoBrainer so far?

@robertjpayne
Copy link
Author

@nviennot really good. I'm pretty new to building backend API's so I can't really compare too much.

I think the only really hard part I've had to deal with is really custom associations that us a lot of RQL to build the association ( but they are reliant on properties in the model instance so they can't just be a scope )

Thanks again for the hard work! I also saw a few tickets over on RethinkDB's issue tracker (2PC, Celluloid) you've been trying to get sorted which is great.

@zedtux zedtux added the enhancement Improve what already exist. label Feb 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve what already exist.
Projects
None yet
Development

No branches or pull requests

3 participants