On Tuesday, September 15, 2015 at 1:15:13 AM UTC-7, Sylver wrote:
>
> 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
>

 For this, I think you should just add a dataset method:

class Post < Sequel::Model
  dataset_module do
    def viewable(user_id, follower_id)
      left_join(:users_followers, :following_id=>:user_id).
        where(Sequel.|({:privacy_id => 1}, :privacy_id => 2, :follower_id 
=> follower_id), :status=>%w'published shared', :user_id=>user_id)
    end
  end
end

dataset = Post.viewable(params[:id].to_i, session[:user_id])

If for some reason that won't work for you, please provide more detail as 
to why.

Thanks,
Jeremy

-- 
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.

Reply via email to