Selecting data with two join tables

hy …

I have a problem with selecting data from my database.
this is my database:

users that has_and_belongs_to_many rights
articles that has_and_belongs_to_many rights
rights that has_and_belongs_to_many users and articles.

If I have a user ID… How can I get the articles which he has the rights
to ?

something like this? :

@article_list = User.find_by_id(0).rights.articles

but this doesn’t work
I hope someone has the answer

You have to do it yourself in Ruby if you do it like that, or you will
need
to write some SQL.

class User < ActiveRecord::Base

def articles
@articles||= self.rights.collect{|right|
right.articles.flatten}.flatten
end

end

That will allow you to do

u = User.find(1)
@articles = u.articles

Or you could write a more complex find_by_sql that would to that for you
which may be more efficient.

@William: Yeah. Good catch. I forgot to add that.

THANXS !! (I was strugling with this problem for ages :P)

I would add a .uniq! to the end as well since articles could belong to
multiple rights and so could a user.

def articles
@articles||= self.rights.collect{|right|
right.articles.flatten}.flatten.uniq!
end

-Bill

Brian H. wrote:

end
you which may be more efficient.

rights    that has_and_belongs_to_many  users and articles.

but this doesn't work
I hope someone has the answer
--
Posted via http://www.ruby-forum.com/.


Sincerely,

William P.

I’m pretty sure you can drop the first flatten and also, to avoid the
n+1 query problem, adding :include would almost certainly be more
efficient:

def articles
@articles||= self.rights.find(:all, :include =>
:articles).collect{|right| right.articles }.flatten.uniq!
end

-Bill

William P. wrote:

Brian H. wrote:

end
you which may be more efficient.

rights    that has_and_belongs_to_many  users and articles.


Sincerely,

William P.


Sincerely,

William P.

THANXs for all the replays

but it doesn’t work…
if I try to do it but I get:
undefined method `rights’ for #ApplicationController:0x22a8eb8

I do this:
in my application controller:
@article_list ||= self.rights.find(:all, :include =>:articles)
.collect{|right| right.articles }.flatten.uniq!

and then it will do: (of course in a layout and not in the controller
:P)
<% for article in @article_list %>

<% end %>

does anyone now what the problem is here ?
and yes my models work (I can without problems ask for al the articles
with right is 1 etc.)

In your User model, add this:

def articles
@article_list ||= self.rights.find(:all, :include
=>:articles).collect{|right| right.articles }.flatten.uniq!
end

Then in your controller, call the articles method on the current user.
Say you have @user in your controller, then this would work:

@user.articles

Note that the first method goes in the User model, not in your
controller.

-Bill

Heldop S. wrote:

with right is 1 etc.)


Sincerely,

William P.

Ok well I am an idiot … beceause I can’t read :frowning:
(it was explained in the first post)

Sorry and thanxs guys