I''m new to RoR and databases in general. Here is my problem. I have "Lists" and "Items". Instead of using a join table and a habtm relationship, I created a third model so I can hold additional data in it. It''s called "Listitems". I want to be able to display only the "Items" that have not yet been added to the certain "List". I have: class List < ActiveRecord::Base has_many :listitems, :dependent => true has_many :items, :through => :listitems end class Item < ActiveRecord::Base has_many :listitems, :dependent => true has_many :lists, :through => :listitems end class Listitem < ActiveRecord::Base belongs_to :list belongs_to :item end What kind of query should I be writing in my controller to get the results I want. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Untested, but I think that should do it: class List OTHER_ITEMS_SQL = <<-EOF select * from items where not exists (select * from list_items.item_id = items.id and list_items.list_id = ?) EOF # Get all items that do NOT belong to this list. def other_items Item.find_by_sql [OTHER_ITEMS_SQL, self.id] end end On 17 Nov., 10:19, Hoolio <hooliosph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''m new to RoR and databases in general. Here is my problem. > > I have "Lists" and "Items". Instead of using a join table and a habtm > relationship, I created a third model so I can hold additional data in > it. It''s called "Listitems". I want to be able to display only the > "Items" that have not yet been added to the certain "List". > > I have: > > class List < ActiveRecord::Base > has_many :listitems, :dependent => true > has_many :items, :through => :listitems > end > > class Item < ActiveRecord::Base > has_many :listitems, :dependent => true > has_many :lists, :through => :listitems > end > > class Listitem < ActiveRecord::Base > belongs_to :list > belongs_to :item > end > > What kind of query should I be writing in my controller to get the > results I want.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hoolio wrote:> I''m new to RoR and databases in general. Here is my problem. > > I have "Lists" and "Items". Instead of using a join table and a habtm > relationship, I created a third model so I can hold additional data in > it. It''s called "Listitems". I want to be able to display only the > "Items" that have not yet been added to the certain "List". > > I have: > > class List < ActiveRecord::Base > has_many :listitems, :dependent => true > has_many :items, :through => :listitems > end > > class Item < ActiveRecord::Base > has_many :listitems, :dependent => true > has_many :lists, :through => :listitems > end > > class Listitem < ActiveRecord::Base > belongs_to :list > belongs_to :item > end > > > What kind of query should I be writing in my controller to get the > results I want.Let me encourage you to go "fat model, skinny controller" and write that query in the model''s class instead. Just call that method from the controller. You''ll thank yourself later. Also, if I were doing it, I''d name your join model "Listing", which falls more trippingly from the tongue than "ListItem". As for the query, I show how to do that sort of thing here: http://blog.hasmanythrough.com/2006/9/9/finding-unassociated-objects -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I think I''m still misunderstanding. I read the blogpost and tried doing: class List < ActiveRecord::Base has_many :items, :through => :listitems do def not find(:conditions => ["items.id != ?", proxy_owner.id]) end end end I then try looping through <% for item in @list.items.not %> <% item.name %> <% end%> I get the error "Couldn''t find Item without an ID" On Nov 17, 10:51 am, Josh Susser <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hoolio wrote: > > I''m new to RoR and databases in general. Here is my problem. > > > I have "Lists" and "Items". Instead of using a join table and a habtm > > relationship, I created a third model so I can hold additional data in > > it. It''s called "Listitems". I want to be able to display only the > > "Items" that have not yet been added to the certain "List". > > > I have: > > > class List < ActiveRecord::Base > > has_many :listitems, :dependent => true > > has_many :items, :through => :listitems > > end > > > class Item < ActiveRecord::Base > > has_many :listitems, :dependent => true > > has_many :lists, :through => :listitems > > end > > > class Listitem < ActiveRecord::Base > > belongs_to :list > > belongs_to :item > > end > > > What kind of query should I be writing in my controller to get the > > results I want. > > Let me encourage you to go "fat model, skinny controller" and write that > query in the model''s class instead. Just call that method from the > controller. You''ll thank yourself later. Also, if I were doing it, I''d > name your join model "Listing", which falls more trippingly from the > tongue than "ListItem". > > As for the query, I show how to do that sort of thing here:http://blog.hasmanythrough.com/2006/9/9/finding-unassociated-objects > > -- > Josh Susserhttp://blog.hasmanythrough.com > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I ended up using in my view: <% for item in @items %> <% unless @list.items.include?(item) %> [CODE] etc etc etc It''s messy, but it seems to work On Nov 17, 1:46 pm, Hoolio <hooliosph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I think I''m still misunderstanding. I read the blogpost and tried > doing: > > class List < ActiveRecord::Base > has_many :items, :through => :listitems do > def not > find(:conditions => ["items.id != ?", proxy_owner.id]) > end > end > end > > I then try looping through > > <% for item in @list.items.not %> > <% item.name %> > <% end%> > > I get the error "Couldn''t find Item without an ID" > > On Nov 17, 10:51 am, Josh Susser <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Hoolio wrote: > > > I''m new to RoR and databases in general. Here is my problem. > > > > I have "Lists" and "Items". Instead of using a join table and a habtm > > > relationship, I created a third model so I can hold additional data in > > > it. It''s called "Listitems". I want to be able to display only the > > > "Items" that have not yet been added to the certain "List". > > > > I have: > > > > class List < ActiveRecord::Base > > > has_many :listitems, :dependent => true > > > has_many :items, :through => :listitems > > > end > > > > class Item < ActiveRecord::Base > > > has_many :listitems, :dependent => true > > > has_many :lists, :through => :listitems > > > end > > > > class Listitem < ActiveRecord::Base > > > belongs_to :list > > > belongs_to :item > > > end > > > > What kind of query should I be writing in my controller to get the > > > results I want. > > > Let me encourage you to go "fat model, skinny controller" and write that > > query in the model''s class instead. Just call that method from the > > controller. You''ll thank yourself later. Also, if I were doing it, I''d > > name your join model "Listing", which falls more trippingly from the > > tongue than "ListItem". > > > As for the query, I show how to do that sort of thing here:http://blog.hasmanythrough.com/2006/9/9/finding-unassociated-objects > > > -- > > Josh Susserhttp://blog.hasmanythrough.com > > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---