I''d create a table (as persons) related to another (as animals), to selecting the animals prefered for each person but indicating too wich is the most prefered. My idea would be use ''has_many'' to relating some animals and ''has_one'' for the most prefered (that would not be in the another relation). Do you know another solution better that this? Thanks in advance! ----------- create_table :persons do |t| ... has_many :animals # Animales prefered has_one :animals # Animal most prefered end create_table :animals do |t| t.string :name ... belongs_to :persons end ----------- --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
two of them: 1) make it an acts_as_list and allow to sort animals by preference (but lots of more work) 2) just have a flag prefered in animals then in persons class Person < ActiveRecord::Base has_many :animals do def prefered find(:first, :conditions => {:prefered => true}) end end -- 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 -~----------~----~----~----~------~----~------~--~---
On 22 feb, 10:10, Thorsten Mueller <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 2) just have a flag prefered in animals > then in persons > > class Person < ActiveRecord::Base > has_many :animals do > def prefered > find(:first, :conditions => {:prefered => true}) > end > endBut with this option, does each person could have a different prefered animal? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Kless wrote:> On 22 feb, 10:10, Thorsten Mueller <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> 2) just have a flag prefered in animals >> then in persons >> >> class Person < ActiveRecord::Base >> has_many :animals do >> def prefered >> find(:first, :conditions => {:prefered => true}) >> end >> end > But with this option, does each person could have a different prefered > animal?the way you described your setup: animal belongs_to person would mean, that a animal record can belong to only one person so i assumed, that you talk about ''named'' animals so a person has a cat named "foo" another person a cat named "bar" if you want to have a set of animals (horse, cat, dog) of which each person can chose some and set one as prefered you would need a habtm relationship each person can have many animals each animal can belong to many persons in your case, where you need additional information (prefered) you should create an intermediate model PersonsAnimals belongs_to :person has_many :animals and keep the prefered flag and it''s finder method in this model and of course you can make use og :through Person has_many :persons_animals has_many :animals :through => :persons_animals -- 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 -~----------~----~----~----~------~----~------~--~---
Kless wrote:> But with this option, does each person could have a different prefered > animal?Not easily. There is no way (AFAIK) to put a relationship between two relations in Rails that enables the related has_many model to remain independent of the primary model. I''m happy to be proven wrong... One solution is to override methods in your Person model for setting the animals and preferred animal. These methods can do the validation, so setting the preferred animal checks it is contained in the animals list and removing an animal from the animals list can check if it is the preferred animal (and either fail or remove the preferred animal depending on how you want it to function). Your unit tests will be important here... -- 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 -~----------~----~----~----~------~----~------~--~---
Mark Bush wrote:> Not easily. There is no way (AFAIK) to put a relationship between two > relations in Rails that enables the related has_many model to remain > independent of the primary model. I''m happy to be proven wrong...OK, yes, I missed the fact this is really (by the sound of it) a has_and_belongs_to_many relationship and so Thorsten is obviously right. An intermediate relationship is the way to go. Preference is then defined there. -- 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 -~----------~----~----~----~------~----~------~--~---
Thank you very much for the help. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---