Thanks Jeremy,

   I am planning to rework the schema eventually, but am just trying
to see if I can tweak the models around the current db without
modifications so I better understand it.

   The custom association code that you gave worked great in fact and
it loaded up the associated model too.

>> Attendance.many_to_one :child, :key=>:child_id, :primary_key=>:id do |ds|
  ds.filter(:acad_year=>'2009-2010')
end
>> af=Attendance.first
=> #<Attendance @values={:class=>"2", :child_id=>1, :week=>#<Date:
4910175/2,0,2299161>, :present=>"no"}>
>> af.child_dataset
=> #<Sequel::MySQL::Dataset: "SELECT * FROM `child` WHERE
((`child`.`id` = 1) AND (`acad_year` = '2009-2010'))">
>> af.child
=> #<Child @values={:active=>1, ...">

Thanks again! Now back to understanding why it works!

What is custom :eager_loader BTW ?

--
Thanks!
Journeyer


On Nov 9, 2:23 pm, Jeremy Evans <[email protected]> wrote:
> On Nov 9, 2:03 pm, Journeyer <[email protected]> wrote:
>
>
>
> > Hi,
>
> >    I am just getting started with sequel. I looked but didn't find
> > anything similar in the archives or docs.
>
> > I am trying to make sequel work with some legacy mysql tables. I am
> > having the following tables:
>
> > Child (primary key- composite of :id and :acad_year) and other fields
> > Parent(primary key - composite of :id and :acad_year) and other fields
> > Attendance(primary key - composite of child_id, class)
>
> > (Basically child has many to one relationship with parent, child has
> > one to many relationship with attendance)
>
> > I have the following model definitions:
>
> > class Parent < Sequel::Model(:parents)
> >   set_primary_key :id, : acad_year
>
>     one_to_many :children> end
> > class Child < Sequel::Model(:child)
> >   set_primary_key :id, :acad_year
>
>     many_to_one :parent> end
> > class Attendance < Sequel::Model(:attendance)
> >   many_to_one :child, :key=>'child_id'
>
>     many_to_one :child
>
> > end
>
> > So I expect to be able to load the Child instance directly from
> > Attendace like this:>> af=Attendance.first
>
> > => #<Attendance @values={:class=>"2", :child_id=>1, :week=>#<Date:
> > 4910175/2,0,2299161>, :present=>"no"}>>> af.child_id
> > => 1
>
> Here's the problem.  You don't have the acad_year as a column in the
> attendence table. You'll have to better describe your schema.  From
> what you've provided, I'm guessing one of two things:
>
> 1) The :id in the child/parent tables are themselves unique, and you
> don't need a composite primary key in either.  If that is the case,
> just don't use composite keys.
>
> 2) The :id in the child/parent table are not unique, but are unique
> for a given academic year, so you need composite keys.  In that case,
> attendence must also have a acad_year field in order for there to be a
> relationship between it and the child table.  It looks like you may be
> able to determine the acad_year from the week, but that's not going to
> be an optimal approach.
>
> If you have control over the schema, switch to using single primary
> keys if possible, or adding the acad_year to attendance if not.  If
> you don't have control over the schema, you'll need to write a custom
> association, maybe something like:
>
>   Attendance.many_to_one :child, :key=>:child_id, :primary_key=>:id do
> |ds|
>     ds.filter(:acad_year=>week.year)
>   end
>
> That won't work for eager loading though (you'd need to use a database
> function), so you'd need to write a custom :eager_loader for that.
>
> Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to