Here''s a question from a newbie. I''m playing with a user-based feed aggregator of sorts which uses the following three main tables: feeds (id, name, url, etag, ...) user (id, email, password, ...) user_feeds (id, user_id, feed_id, custom_title, display_summaries ..) Basically, the feeds table includes general data about all feeds registered in the system, and user_feeds creates the relationships between users and feeds (which users are subscribed to what feeds). So I have the following models: Feed, UserFeeds, and the controller in question is MyFeeds, which retrieves data mainly from UserFeeds. class MyFeedsController < ApplicationController def index @my_feeds = UserFeeds.my_feeds(@session[''user''].id) end end class UserFeeds < ActiveRecord::Base belongs_to :feed def self.my_feeds(id) find(:all, :conditions => [''user_id = ?'', id]) end end Question: how would you improve the my_feeds() class-method? Is my choice of ''belongs_to'' correct? The code seems to work, but I''m left wondering if there is a cleaner way of writing it... --Jonas Galvez
Jonas Galvez <jonas-ojpezf1QQkTMgQYf1r47wg@public.gmane.org> writes:> class MyFeedsController < ApplicationController > def index > @my_feeds = UserFeeds.my_feeds(@session[''user''].id) > end > end > > class UserFeeds < ActiveRecord::Base > belongs_to :feed > def self.my_feeds(id) > find(:all, :conditions => [''user_id = ?'', id]) > end > end > > Question: how would you improve the my_feeds() class-method? Is my > choice of ''belongs_to'' correct? The code seems to work, but I''m left > wondering if there is a cleaner way of writing it...class UserFeeds < ActiveRecord::Base belongs_to :feed belongs_to :user end class User < ActiveRecord::Base has_many :feeds end Then just do @user.feeds to get the list of feeds. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
Hi Jonas, I am new to rails as well, but another way you could try is to setup your associations using has_and_belongs_to_many for your users and feeds. You would need to rename your join table to ''feeds_users'' (it needs to be both plural and in alpha order). Then, to get a users feeds you could do: @user = User.find(user_id) @user.feeds # get the feeds... I believe the @user.feeds will also return all the other data that is in the join table. Your UserFeeds controller may not be necessary if you go this route. More information can be found here: http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000429 Tom On 7/8/05, Jonas Galvez <jonas-ojpezf1QQkTMgQYf1r47wg@public.gmane.org> wrote:> Here''s a question from a newbie. I''m playing with a user-based feed > aggregator of sorts which uses the following three main tables: > > feeds (id, name, url, etag, ...) > user (id, email, password, ...) > user_feeds (id, user_id, feed_id, custom_title, display_summaries ..) > > Basically, the feeds table includes general data about all feeds > registered in the system, and user_feeds creates the relationships > between users and feeds (which users are subscribed to what feeds). > > So I have the following models: Feed, UserFeeds, and the controller in > question is MyFeeds, which retrieves data mainly from UserFeeds. > > class MyFeedsController < ApplicationController > def index > @my_feeds = UserFeeds.my_feeds(@session[''user''].id) > end > end > > class UserFeeds < ActiveRecord::Base > belongs_to :feed > def self.my_feeds(id) > find(:all, :conditions => [''user_id = ?'', id]) > end > end > > Question: how would you improve the my_feeds() class-method? Is my > choice of ''belongs_to'' correct? The code seems to work, but I''m left > wondering if there is a cleaner way of writing it... > > --Jonas Galvez > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Doug''s way is better (he posted while I was writing mine)... but for a many to many relationship, you could try my suggestion. I am still getting a feel for all of the association magic, but there is a lot of cool stuff in there. Have fun. Tom On 7/8/05, Jonas Galvez <jonas-ojpezf1QQkTMgQYf1r47wg@public.gmane.org> wrote:> Doug Alcorn wrote: > > class UserFeeds < ActiveRecord::Base > > belongs_to :feed > > belongs_to :user > > end > > > > class User < ActiveRecord::Base > > has_many :feeds > > end > > > > Then just do @user.feeds to get the list of feeds. > > Damn, that''s sweet. Thanks a lot. > > --Jonas Galvez > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Doug Alcorn wrote:> class UserFeeds < ActiveRecord::Base > belongs_to :feed > belongs_to :user > end > > class User < ActiveRecord::Base > has_many :feeds > end > > Then just do @user.feeds to get the list of feeds.Damn, that''s sweet. Thanks a lot. --Jonas Galvez
Tom Davies <atomgiant-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:> Doug''s way is better (he posted while I was writing mine)... but for a > many to many relationship, you could try my suggestion. I am still > getting a feel for all of the association magic, but there is a lot of > cool stuff in there.It''s true that this is a many to many relationship. Users have many feeds and feeds belong to many users. It doesn''t take long reading the mailing list or listening to irc to realize that has_and_belongs_to_many is about as complicated as Rails gets. Sometimes it''s much simpler to make these intermediate classes that are one to many. Rather than: Users <-> Feeds you get: Users <- UserFeeds -> Feeds While this second set of relationships seems more complicated, it''s actually easier to keep up with. This is particularly true when you have attributes on the join object. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
> Sometimes it''s much simpler to make these intermediate classes that > are one to many. Rather than: > > Users <-> Feeds > > you get: > > Users <- UserFeeds -> Feeds > > While this second set of relationships seems more complicated, it''s > actually easier to keep up with. This is particularly true when you > have attributes on the join object.I agree to that. I also prefer to work that way. I only use has_and_belongs_to_many on very specific, simple cases. rgds Dema -- http://dema.ruby.com.br - Rails from a .NET perspective