ISSUE: The host has 3 ip addresses on two networks (#62 is repeated) How do I get host.networks to return only the two networks?>> host=> #<NetworkHost id: 923, name: "ota-app1", os_id: 1, feature_id: nil, description: nil, created_at: "2010-05-18 19:05:49", updated_at: "2010-05-18 19:05:49", ip_id: nil, server_id: 5, service_tag: nil, location_id: nil, rack: nil, space: nil, net_profile_id: 77>>> host.ip_addresses.size=> 3>> host.networks=> [#<Network id: 62, name: "SCTCarrier990 Production Network", ip: "192.168.110.0", ...l>, #<Network id: 62, name: "SCTCarrier990 Production Network", ip: "192.168.110.0", ...>, #<Network id: 63, name: "SCTCarrier990 IP Profile Network3", ip: "10.10.110.0", ...>]>> host.networks.size=> 3 ## networks model class Network < ActiveRecord::Base has_many :ip_addresses, :dependent => :destroy has_many :network_hosts, :through => :ip_addresses ## network_hosts model class NetworkHost < ActiveRecord::Base has_many :ip_addresses, :dependent => :destroy has_many :networks, :through => :ip_addresses ## ip_addresses model class IpAddress < ActiveRecord::Base attr_accessible :name, :ip, :logical_interface_name_id, :network_host_id, :network_id, :is_entry_point, :is_gateway, :mac belongs_to :network_host belongs_to :network -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hassan Schroeder
2010-May-27 21:14 UTC
Re: has_many :through association not working properly
On Thu, May 27, 2010 at 10:41 AM, chewmanfoo <chewmanfoo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> ISSUE: > The host has 3 ip addresses on two networks (#62 is repeated) > How do I get host.networks to return only the two networks?> ## network_hosts model > class NetworkHost < ActiveRecord::Base > > has_many :ip_addresses, :dependent => :destroy > has_many :networks, :through => :ip_addressesWouldn''t has_many :networks, :through => :ip_addresses, :uniq => true do it? -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org twitter: @hassan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Yes! that''s it! thanks! On May 27, 4:14 pm, Hassan Schroeder <hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Thu, May 27, 2010 at 10:41 AM, chewmanfoo <chewman...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > ISSUE: > > The host has 3 ip addresses on two networks (#62 is repeated) > > How do I get host.networks to return only the two networks? > > ## network_hosts model > > class NetworkHost < ActiveRecord::Base > > > has_many :ip_addresses, :dependent => :destroy > > has_many :networks, :through => :ip_addresses > > Wouldn''t > > has_many :networks, :through => :ip_addresses, :uniq => true > > do it? > -- > Hassan Schroeder ------------------------ hassan.schroe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > twitter: @hassan-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.