Forum: Ruby on Rails belongs_to and has_many

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
A5e8de087acdf4908cc7cd9c8ecb2ae9?d=identicon&s=25 Blake Lewis (blake)
on 2006-04-25 21:19
    I know this has been rehashed many times, but I don't get it.
I can't find it documented to were I understand all the parts. I can
make this work using Rails database design conventions, but in this case
I am not able to do that.
    I find examples, but the ones I find do not follow the conventions.
They also do not give enough information to understand what is going on.

In the model:
   What is the "association"? Is it the field name minus the _id?
   :foreign_key is the key in the other table.
   :class_name is the name of the model of the other table.
  admin VARCHAR(32) NOT NULL,
CREATE TABLE machines(
  padmin_id REFERENCES admins(admin),
  sadmin_id REFERENCES admins(admin)
class Machine < ActiveRecord::Base
  belongs_to :padmin, :class_name => "admin", :foreign_key => "id"
  belongs_to :sadmin, :class_name => "admin", :foreign_key => "id"
class Admin < ActiveRecord::Base
  has_many :machines, :foreign_key => 'padmin_id'
  has_many :machines, :foreign_key => 'sadmin_id'
If it were a straight association machine.admin_id and, then
this works
<td><%= list_stripes.admin ? list_stripes.admin["admin"] : "UnAssigned"
I would be happy if I could understand it well enough that I could use
it without the compilcation of "sadmin_id".

882cc23c77c5c6d27613c51396a02a0d?d=identicon&s=25 Stephen Bartholomew (Guest)
on 2006-04-25 22:47
(Received via mailing list)
I'm not sure i quite understand your data model...

If it's: "One admin can have many machines, One machine belongs to many
admins" then you should have a joiner table:

administrators      administrators_machines       machines
--------------      -----------------------       --------
id            <---- administrator_id         |--> id
admin               machine_id             --     machine
--------------      -----------------------       ---------

Your models will then reflect this relationship:

class Machine < ActiveRecord::Base
   has_and_belongs_to_many :administrators

class Administrator < ActiveRecord::Base
   has_and_belongs_to_many :machines

Rails will automatically map everything together for you.  The
associations should be avaliable as a collection in the models:


Hopefully this will be helpful - i apologise if i've mis-understood your
problem :0)

A5e8de087acdf4908cc7cd9c8ecb2ae9?d=identicon&s=25 Blake Lewis (blake)
on 2006-04-26 22:38
    I think you understand my data. I was trying to avoid habtm. I
haven't found a way to associate data with each join. We have primary
and secondary admins for each machine.

habtm does not deal with this:

administrators      administrators_machines       machines
--------------      -----------------------       --------
id            <---- administrator_id         |--> id
admin               machine_id             --     machine
                    [primary | secondary]
--------------      -----------------------       ---------
    In the mean time I would like to figure out the correct syntax for
describing  the models and listing the data with just belongs_to and
has_many. I would drop the secondary admin out of the model. But instead
of renameing things I would like to know how to do it without following
rails naming conventions.

Does join through allow this type of model?

4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2006-04-27 00:17
Blake Lewis wrote:

> Does join through allow this type of model?
> Blake

The :through approach requires creating a new Model in ghe middle, say

 so   administrator has_many :adminships and has_many :machines :though
=> 'adminships'
and  machine has_many :adminships and has_many :administrators :through
=> 'adminships'.

The adminships table, in addition to administrayor_id and machine_id
will have, say a priority column indicating primary or secondary

The Adminship model would belongs_to :machine and belongs_to

More blurb here:

Does this help ?

This topic is locked and can not be replied to.