Hello there,
I'm struggling on how to retrieve a dataset in Sequel for a usual pattern
of posts list, based on their privacy and the relationship between the
owner of the posts and the one getting the list.
The final raw sql query should look like this :
SELECT p.* FROM users_posts p
LEFT JOIN users_followers f ON p.user_id=f.following_id
WHERE p.status IN ('published', 'shared') AND p.user_id=123
AND (p.privacy_id=1 or (p.privacy_id=2 and f.follower_id=456));
with :
- privacy_id=1 => public
- privacy_id=2 => friends/followers only
- p.user_id => the user the posts belong to
- f.follower_id => the id of the user retrieving the list
So far i have (simplified) :
class User < Sequel::Model
many_to_many :followings, :left_key=>:follower_id, :right_key=>:
following_id, :join_table=>:users_followers, :class => self
many_to_many :followers, :left_key=>:following_id, :right_key=>:
follower_id, :join_table=>:users_followers, :class => self
end
class Post < Sequel::Model
many_to_one :user
end
And i'm trying to build the dataset like this :
dataset = Post.eager(:user => {:followers => proc{ |ds| ds.where{
follower_id == session[:user_id] }}})
dataset = dataset.where(:user_id => params[:id])
dataset = dataset.where{Sequel.|({:privacy_id => 1}, Sequel.&({:privacy_id
=> 2}, {:follower_id => session[:user_id]}))}
Without the fact that i'm thinking i'm far from the right way to do it,
Sequel doesn't understand the :follower_id anyway, which is not in the
posts table, i don't know how to make it understands it is in the eagerly
loaded followers relationship.
Some help would be greatly appreciated
Thanks
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.