[Rails] Associations

"Luis G. Gómez" lgomez at vfxnetwork.com
Fri Nov 26 10:28:26 GMT 2004


I posted this here http://www.sitepoint.com/forums/showthread.php?t=212771

Just in case.

Luis G. Gómez wrote:
> Hello again...
> 
> The idea behind this app is to be able to serve certail files to certain 
> users. To assign files to users you can create a group and add users and 
> files and/or explicitly assign or exclude files for a particular user.
> 
> So, I grab the user's id at login. I fetch the groups to which the user 
> belongs to in order to find the files he/she has access to. After I know 
> which files the user has access to based on the groups, I check for 
> entries in the documents_users table. Entries in documents_users have a 
> flag (documents_users.switch) that is eithar 0 or 1 based on which I add 
> or remove files from the users accessible file list (0=remove and 
> 1=add). At the end I just render the list of files so the use can 
> download it. The rendering part is done, what I don't get are the files. 
> I can't find a way to have the view render @user.documents based on the 
> described criteria.
> 
> Here are my model deffinitions (table deffinitions are bellow):
> 
> User Model
>       1 require 'active_record'
>       2
>       3 class User < ActiveRecord::Base
>       4         has_and_belongs_to_many :documents, :conditions => 
> "switch=1"
>       5         has_and_belongs_to_many :groups
>       6         def self.authenticate(name,password)
>       7                 find_first(["alias='%s' AND 
> password='%s'",name,password])
>       8         end
>       9 end
> 
> Group Model
>       1 require 'active_record'
>       2
>       3 class Group < ActiveRecord::Base
>       4         has_many :users
>       5         has_many :documents
>       6 end
> 
> Document Model
>       1 require 'active_record'
>       2
>       3 class Document < ActiveRecord::Base
>       4         has_and_belongs_to_many :users
>       5         has_and_belongs_to_many :groups
>       6         def self.user_documents(user)
>       7                 find_by_sql("SELECT * FROM groups WHERE 
> name='#{user}'")
>       8         end
>       9 end
> 
> 
> //////////TABLES//////////
> CREATE TABLE `documents` (
>   `id` int(11) NOT NULL auto_increment,
>   `name` varchar(100) NOT NULL default '',
>   `size` float NOT NULL default '0',
>   `last_mod` timestamp NOT NULL,
>   `mime` varchar(100) NOT NULL default '',
>   `document` blob NOT NULL,
>   PRIMARY KEY  (`id`)
> ) TYPE=MyISAM DEFAULT CHARSET=latin1;
> 
> CREATE TABLE `documents_groups` (
>   `group_id` int(11) NOT NULL default '0',
>   `document_id` int(11) NOT NULL default '0',
>   PRIMARY KEY  (`group_id`,`document_id`)
> ) TYPE=MyISAM DEFAULT CHARSET=latin1;
> 
> CREATE TABLE `documents_users` (
>   `document_id` int(11) NOT NULL default '0',
>   `user_id` int(11) NOT NULL default '0',
>   `switch` tinyint(4) NOT NULL default '0',
>   PRIMARY KEY  (`document_id`,`user_id`)
> ) TYPE=MyISAM DEFAULT CHARSET=latin1;
> 
> CREATE TABLE `groups` (
>   `id` int(11) NOT NULL auto_increment,
>   `name` varchar(100) NOT NULL default '',
>   PRIMARY KEY  (`id`),
>   KEY `group_name` (`name`)
> ) TYPE=MyISAM DEFAULT CHARSET=latin1;
> 
> CREATE TABLE `groups_users` (
>   `switch` tinyint(4) NOT NULL default '0',
>   `group_id` int(11) NOT NULL default '0',
>   `user_id` int(11) NOT NULL default '0',
>   PRIMARY KEY  (`group_id`,`user_id`)
> ) TYPE=MyISAM DEFAULT CHARSET=latin1;
> 
> CREATE TABLE `users` (
>   `admin` tinyint(1) NOT NULL default '0',
>   `id` int(11) NOT NULL auto_increment,
>   `alias` varchar(100) NOT NULL default '',
>   `password` varchar(100) NOT NULL default '',
>   PRIMARY KEY  (`id`),
>   UNIQUE KEY `user` (`alias`)
> ) TYPE=MyISAM DEFAULT CHARSET=latin1;
> //////////ENDTABLES//////////
> 
> Arent the has_and_belongs_to_many associations supposed to unse a 
> relationship table to perform joins? In this case tables 
> documents_groups, documents_users and groups_users.
> 
> Hope you see what I'm missing... :S
> 
> Thank you!!
> 
> LG
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
> 


More information about the Rails mailing list