On Jan 12, 7:44 pm, Josh Bassett <[email protected]> wrote:
> Hi there
>
> I've run into a problem where loading models through a many_to_many
> association clobbers the dataset defined in the model class.
>
> For example:
>
> class Company < Sequel::Model
>   many_to_many :employees, :join_table => :employments, :left_key
> => :company_id, :right_key => :person_id
> end
>
> class Person < Sequel::Model
>   set_dataset select(:full_name => :name)
> end
>
> In irb:
>
> > Person.first    # {:name => "John Appleseed"}
> > Company.first.employees.first    # {:full_name => "John Appleseed"} but 
> > should be {:name => "John Appleseed"}
>
> If I change the many_to_many to alias the columns I get the expected
> result, but this is undesirable as I have duplicated the aliases
> across two classes:
>
> class Company < Sequel::Model
>   many_to_many :employees, :join_table => :employments, :left_key
> => :company_id, :right_key => :person_id, :select => [:full_name
> => :name]
> end
>
> How do I get the association to use the default dataset defined in the
> Person class when loading the records? Am I going about this the wrong
> way?

Nope, you do have to define it twice.  A many_to_many dataset doesn't
use the associated model's dataset, it uses a join between the
associated model's table and the join table.

Honestly, I wouldn't recommend aliasing the column like that, it'll
probably do more harm than good.  Just add the following to the model
class:

  class Person < Sequel::Model
    alias name full_name
    alias name= full_name=
  end

With that, you'll be able to call name and name= methods and still use
full_name as the backing column.  You will still have to
use :full_name in filters/orders/etc., though.

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