Friday, 15 January 2010

Ruby on Rails Data Modeling -



Ruby on Rails Data Modeling -

i have user table next schema:

user -- id , name , age etc

and ride table next schema:

ride -- id , , etc.

i have bookings table schema: booking - id, user_id, ride_id

is there way can describe details ride from , etc , details user made booking?

assuming have next relationship:

class user < activerecord::base has_many :bookings has_many :rides, :through => :bookings end class booking < activerecord::base belongs_to :user belongs_to :ride end class ride < activerecord::base has_many :bookings has_many :users, :through => :bookings end

you can retrieve info want.

booking = booking.find(1) booking.user.name => #return user name booking.ride.from => #return ride etc

furthermore, :through allows access user straight ride , vice-versa:

user = user.find(1) user.rides => #return rides user

ruby-on-rails rails-activerecord

No comments:

Post a Comment