Hi! In one of my current projects, I have an habtm relationship between two models. To access the table via console, I created the model for the join table. so for example, I have class Foo has_and_belongs_to_many :bars, :join_table => ''foo_bars'' I created a FooBar class so I can actually access the records via AR. My question is this, is it fine to add another column to this table? -- 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.
On Wed, Mar 30, 2011 at 7:24 PM, Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi! > > In one of my current projects, I have an habtm relationship between two > models. To access the table > via console, I created the model for the join table. so for example, I > have > > class Foo > has_and_belongs_to_many :bars, :join_table => ''foo_bars'' > > I created a FooBar class so I can actually access the records via AR. My > question is this, is it fine to > add another column to this table? >If you are going to add another column to the FooBar table then you might as well change the associations of models Foo and Bar to has_many :foo_bar and then put belongs_to :foo belongs_to :bar in your model FooBar. The point of the has_and_belongs_to_many is to setup a join table for association that you don''t plan on accessing through a model or need additional fields for. B. -- 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.
Using HABTM prevents you from using the additional column, but you can get the desired effect by replacing HABTM on each side with something like: class Foo has_many :foo_bars, :join_table => "foo_bars" has_many :bars, :through => :foo_bars end On Thu, Mar 31, 2011 at 2:34 AM, Bryan Crossland <bacrossland-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> On Wed, Mar 30, 2011 at 7:24 PM, Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Hi! >> >> In one of my current projects, I have an habtm relationship between two >> models. To access the table >> via console, I created the model for the join table. so for example, I >> have >> >> class Foo >> has_and_belongs_to_many :bars, :join_table => ''foo_bars'' >> >> I created a FooBar class so I can actually access the records via AR. My >> question is this, is it fine to >> add another column to this table? >> > > If you are going to add another column to the FooBar table then you might > as well change the associations of models Foo and Bar to has_many :foo_bar > and then put belongs_to :foo belongs_to :bar in your model FooBar. The point > of the has_and_belongs_to_many is to setup a join table for association that > you don''t plan on accessing through a model or need additional fields for. > > B. > > -- > 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. >-- 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.
On Thu, Mar 31, 2011 at 1:18 PM, Chris Kottom <chris-kMviOf/NVQxZWXO/OqhO/A@public.gmane.org> wrote:> Using HABTM prevents you from using the additional column, but you can get > the desired effect by replacing HABTM on each side with something like: > >I''ve ended up adding a new column to foo_bars which I can access fine without changing the current relationships. The problem is with updating a FooBar record which has no ID column which rails needs. Since I don''t need an ID column on foo_bars, i just update the record with an sql statement. Cheers!> class Foo > has_many :foo_bars, :join_table => "foo_bars" > has_many :bars, :through => :foo_bars > end > > > On Thu, Mar 31, 2011 at 2:34 AM, Bryan Crossland <bacrossland-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: > >> On Wed, Mar 30, 2011 at 7:24 PM, Jim Ruther Nill <jvnill-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote: >> >>> Hi! >>> >>> In one of my current projects, I have an habtm relationship between two >>> models. To access the table >>> via console, I created the model for the join table. so for example, I >>> have >>> >>> class Foo >>> has_and_belongs_to_many :bars, :join_table => ''foo_bars'' >>> >>> I created a FooBar class so I can actually access the records via AR. My >>> question is this, is it fine to >>> add another column to this table? >>> >> >> If you are going to add another column to the FooBar table then you might >> as well change the associations of models Foo and Bar to has_many :foo_bar >> and then put belongs_to :foo belongs_to :bar in your model FooBar. The point >> of the has_and_belongs_to_many is to setup a join table for association that >> you don''t plan on accessing through a model or need additional fields for. >> >> B. >> >> -- >> 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. >> > > -- > 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. >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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-/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.