Hi, I have 2 tables that I had created with migrations, but these are based on existing database. So the automatically created id column is not used. These tables have the following structure. Table calls +-------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+--------------+------+-----+---------+----------------+ | id | int(11) | | PRI | NULL | auto_increment | | src | varchar(80) | YES | | NULL | | | dst | varchar(80) | YES | | NULL | | | ivr_id | int(11) | YES | | NULL | | +-------------+--------------+------+-----+---------+----------------+ and Table clients +-----------+---------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------+------+-----+---------+----------------+ | id | int(11) | | PRI | NULL | auto_increment | | ivr_id | int(11) | YES | | NULL | | | is_online | char(1) | YES | | NULL | | +-----------+---------+------+-----+---------+----------------+ The ivr_id in calls table is the foreign key referring to ivr_id in clients table. How can I specify this relation in RoR model so that rails "understand" which columns to link to when I specify the has_many and belongs_to in respective models. Thanks for reading, raj --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rajkumar S wrote:> Hi, > > I have 2 tables that I had created with migrations, but these are > based on existing database. So the automatically created id column is > not used. These tables have the following structure. > > Table calls > +-------------+--------------+------+-----+---------+----------------+ > | Field | Type | Null | Key | Default | Extra | > +-------------+--------------+------+-----+---------+----------------+ > | id | int(11) | | PRI | NULL | auto_increment | > | src | varchar(80) | YES | | NULL | | > | dst | varchar(80) | YES | | NULL | | > | ivr_id | int(11) | YES | | NULL | | > +-------------+--------------+------+-----+---------+----------------+ > > and Table clients > +-----------+---------+------+-----+---------+----------------+ > | Field | Type | Null | Key | Default | Extra | > +-----------+---------+------+-----+---------+----------------+ > | id | int(11) | | PRI | NULL | auto_increment | > | ivr_id | int(11) | YES | | NULL | | > | is_online | char(1) | YES | | NULL | | > +-----------+---------+------+-----+---------+----------------+ > > The ivr_id in calls table is the foreign key referring to ivr_id in > clients table. How can I specify this relation in RoR model so that > rails "understand" which columns to link to when I specify the > has_many and belongs_to in respective models. > > Thanks for reading, > rajUse the :foreign_key parm in your associations. See the API has_many documentation: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000530 and belongs_to documentation: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000532 c. -- 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 11/10/06, Cayce Balara <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Use the :foreign_key parm in your associations. See the API has_many > documentation:Thanks a lot for your helpful answer. raj --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rajkumar S wrote:> On 11/10/06, Cayce Balara <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> Use the :foreign_key parm in your associations. See the API has_many >> documentation: > > Thanks a lot for your helpful answer. > > rajRaj I would recommend making a few changes to your DB schema if the tables you posted are actually the ones you are using for your application. The structure of the tables you posted opens you up to data integrity issues and unpredictable application behaviour. If you want to use clients.ivr_id as a target for a foreign key reference, you should define a unique constraint on it in your DB. In order to define the foreign key constraint "calls.ivr_id references clients.ivr_id" on your database (which you should do), your DB will require that the target (clients.ivr_id) have a unique constraint on it. Some databases will actually insist that it be the primary key of the referenced table (I believe MySQL does this). Ideally, since you mentioned that you won''t be using the "id" column in the clients table, you should delete it and define ivr_id as the primary key for the clients table. Cheers. - Bryan -- 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 11/11/06, Bryan Evans <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I would recommend making a few changes to your DB schema if the tables > you posted are actually the ones you are using for your application. The > structure of the tables you posted opens you up to data integrity issues > and unpredictable application behaviour.Thanks for the heads up. The clients.ivr_id is supposed to be a unique field so I should have a unique constraint on it.> Ideally, since you mentioned that you won''t be using the "id" column in > the clients table, you should delete it and define ivr_id as the primary > key for the clients table.Is there a "rails" way to do this, ie via a migration. as far as I know the id is created automatically, so there must be some way to disable it and assign another column as the key field. raj raj --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---