Hi, I''m working with two models that have a "has_and_belongs_to_many" relation. Both models reside on a database that it is not the default one. A < External has_and_belongs_to_many: B B < External has_and_belongs_to_many: A Retriving the information for A works perfectly, but when trying to "create" an element on A: "A.B.create(...)" ActiveRecord tries to create it on the default database. The solution was doing: b = B.find(...) A.B << b But, looking on the sourcecode I''ve found on the ActiveRecord file "has_and_belongs_to_many_association.rb [25:29]" def has_primary_key? return @has_primary_key unless @has_primary_key.nil? @has_primary_key (ActiveRecord::Base.connection.supports_primary_key? && ActiveRecord::Base.connection.primary_key(@reflection.options [:join_table])) end Shouln''t it be something like? def has_primary_key? return @has_primary_key unless @has_primary_key.nil? @has_primary_key = (@owner.connection.supports_primary_key? && @owner.connection.primary_key(@reflection.options [:join_table])) end Thank you
On Oct 16, 10:29 am, erzapito <erzap...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, I''m working with two models that have a "has_and_belongs_to_many" > relation. Both models reside on a database that it is not the default > one. >Have you tried using has many through ? with has many through the join table is also a rails model, so you have a place to say that the join table should be in this other database. Fred> A < External > has_and_belongs_to_many: B > > B < External > has_and_belongs_to_many: A > > Retriving the information for A works perfectly, but when trying to > "create" an element on A: > > "A.B.create(...)" > > ActiveRecord tries to create it on the default database. The solution > was doing: > > b = B.find(...) > A.B << b > > But, looking on the sourcecode I''ve found on the ActiveRecord file > "has_and_belongs_to_many_association.rb [25:29]" > > def has_primary_key? > return @has_primary_key unless @has_primary_key.nil? > @has_primary_key > (ActiveRecord::Base.connection.supports_primary_key? && > ActiveRecord::Base.connection.primary_key(@reflection.options > [:join_table])) > end > > Shouln''t it be something like? > > def has_primary_key? > return @has_primary_key unless @has_primary_key.nil? > @has_primary_key = (@owner.connection.supports_primary_key? && > @owner.connection.primary_key(@reflection.options > [:join_table])) > end > > Thank you
Alexandre Paz Mena
2009-Oct-16 10:39 UTC
Re: has_and_belongs_to_many on different databases
On Fri, Oct 16, 2009 at 12:31 PM, Frederick Cheung < frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Have you tried using has many through ? with has many through the join > table is also a rails model, so you have a place to say that the join > table should be in this other database.I tried it, but doing some tests I found that it allowed to have more than one relation between two elements, a new problem that I don''t know how to solve. The real problem was with the library file, deleting elements also fails because it tries to use the base connection instead of the ancestor''s one. -- erzapito-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org @ google talk :D --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Marnen Laibow-Koser
2009-Oct-16 15:46 UTC
Re: has_and_belongs_to_many on different databases
Alexandre Paz Mena wrote:> On Fri, Oct 16, 2009 at 12:31 PM, Frederick Cheung < > frederick.cheung-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> >> Have you tried using has many through ? with has many through the join >> table is also a rails model, so you have a place to say that the join >> table should be in this other database. > > > I tried it, but doing some tests I found that it allowed to have more > than > one relation between two elements, a new problem that I don''t know how > to > solve.validates_uniqueness_of and a unique index in the DB. Done. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.