Forum: Ruby on Rails belongs_to and has_many

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community 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.
------createtables.sql
CREATE TABLE admins(
  id INTEGER PRIMARY KEY,
  admin VARCHAR(32) NOT NULL,
);
CREATE TABLE machines(
  id INTEGER PRIMARY KEY,
  padmin_id REFERENCES admins(admin),
  sadmin_id REFERENCES admins(admin)
)
-------models/machine.rb------------
class Machine < ActiveRecord::Base
  belongs_to :padmin, :class_name => "admin", :foreign_key => "id"
  belongs_to :sadmin, :class_name => "admin", :foreign_key => "id"
end
-------models/admin.rb--------------
class Admin < ActiveRecord::Base
  has_many :machines, :foreign_key => 'padmin_id'
  has_many :machines, :foreign_key => 'sadmin_id'
end
-------views/_list_stripes.rhtml-----
If it were a straight association machine.admin_id and admin.id, then
this works
<td><%= list_stripes.admin ? list_stripes.admin["admin"] : "UnAssigned"
%></td>
-------------------------------------
I would be happy if I could understand it well enough that I could use
it without the compilcation of "sadmin_id".

Blake
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
end

class Administrator < ActiveRecord::Base
   has_and_belongs_to_many :machines
end

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

@administrator.machines.find(:all)

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

Steve
A5e8de087acdf4908cc7cd9c8ecb2ae9?d=identicon&s=25 Blake Lewis (blake)
on 2006-04-26 22:38
Steve,
    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?

Blake
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
'adminship',

 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
:administrator.

More blurb here:
http://blog.hasmanythrough.com/articles/2006/04/20...

Does this help ?

Alan
This topic is locked and can not be replied to.