Hi Colin!

  Thanks for your tips.  :)

  With help of Rails Guides on ActiveRecord Associations I reach this 
point:

= Migrations =

class CreateSchools < ActiveRecord::Migration
  def change
    create_table :schools do |t|
      t.string :name

      t.timestamps
    end
  end
end

class CreateTeams < ActiveRecord::Migration
  def change
    create_table :teams do |t|
      t.integer :school_id
      t.string :name

      t.timestamps
    end
  end
end

class CreatePeople < ActiveRecord::Migration
  def change
    create_table :people do |t|
      t.string :name

      t.timestamps
    end
  end
end

class CreateTeachers < ActiveRecord::Migration
  def change
    create_table :teachers, :id => false do |t|
      t.integer :team_id
      t.integer :person_id

    end
    add_index :teachers, [:team_id, :person_id]
  end
end

class CreateStudents < ActiveRecord::Migration
  def change
    create_table :students, :id => false do |t|
      t.integer :team_id
      t.integer :person_id

    end
    add_index :students, [:team_id, :person_id]
  end
end


= Models =

class School < ActiveRecord::Base
  has_many :teams
end

class Team < ActiveRecord::Base
  belongs_to :school

  has_many :teachers
  has_many :people, :through => :teachers

  has_many :students
  has_many :people, :through => :students
end

class Person < ActiveRecord::Base
end

class Teacher < ActiveRecord::Base
  belongs_to :team
  belongs_to :person
end

class Student < ActiveRecord::Base
  belongs_to :team
  belongs_to :person
end

===

  But after put some test data into the tables I cannot get, for 
example, the names os the students of some team or what are the teachers 
that taught some team.

  Look what I tried:

samsara:first_app leandro$ rails console
Loading development environment (Rails 4.0.0.beta1)
2.0.0-p0 :001 > team = Team.find_by_name("Turma 2A")
  Team Load (0.9ms)  SELECT "teams".* FROM "teams" WHERE "teams"."name" 
= 'Turma 2A' LIMIT 1
 => #<Team id: 4, school_id: 4, name: "Turma 2A", created_at: 
"2013-04-19 12:42:47", updated_at: "2013-04-19 12:42:47">
2.0.0-p0 :002 > team.teachers
  Teacher Load (1.6ms)  SELECT "teachers".* FROM "teachers" WHERE 
"teachers"."team_id" = ?  [["team_id", 4]]
 => #<ActiveRecord::Associations::CollectionProxy [#<Teacher team_id: 4, 
person_id: 2>, #<Teacher team_id: 4, person_id: 15>]>
2.0.0-p0 :003 > team.students
  Student Load (0.2ms)  SELECT "students".* FROM "students" WHERE 
"students"."team_id" = ?  [["team_id", 4]]
 => #<ActiveRecord::Associations::CollectionProxy [#<Student team_id: 4, 
person_id: 8>, #<Student team_id: 4, person_id: 9>, #<Student team_id: 
4, person_id: 11>, #<Student team_id: 4, person_id: 12>]>
2.0.0-p0 :004 >




  I would like to get the People objects instead of the intermediate 
associative tables "teachers" and "students".  ;)


Best regards,
  Mosoleu

Colin Law wrote in post #1106292:
> On 19 April 2013 14:45, Leandro Peracchi <[email protected]> wrote:
>
> The details are not correct as I have not looked at the ER diagram,
> and it is arguable whether you should start with such a diagram in the
> first place, but in order to understand the basics of what rails can
> do for you I suggest you work right through a good tutorial such as
> railstutorial.org, which is free to use online.  Also look at the
> Rails Guides, particularly ActiveRecord Associations.
>
> Colin

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to