Monday, 15 March 2010

mysql - setting up join table in Rails -



mysql - setting up join table in Rails -

so i'm new bring together tables. i'm not sure if that's need. current setup , situation: have 2 user models.

company user

i need create table belongs company model, can answered user model. each company can have several applications called 3rd model

application

so far i've done

class application < activerecord::base belongs_to :company end

and

class company < activerecord::base has_many :applications end

i created application model straight forwards running:

rails g model application

have made mistakes, or doing fine far? , next steps here.

seems far

--

join tables

there 2 types of bring together table - has_and_belongs_to_many & has_many :through

they same thing (create many-to-many relationship), means can phone call @model.objects through join model

from reading information, looks this:

#app/models/user.rb class user < activerecord::base has_many :applications has_many :companies, through: :applications end #app/models/application.rb class application < activerecord::base #fields - id | user_id | company_id | etc | etc | created_at | updated_at belongs_to :company belongs_to :user end #app/models/company.rb class company < activerecord::base has_many :applications has_many :users, through: :applications end

mysql sql ruby-on-rails

No comments:

Post a Comment