On Friday, March 30, 2012 5:53:06 AM UTC-7, Gruffalo wrote:
>
> Hello everyone!
>
>
> I've looked at the Sequel docs (Associations and Advanced Associations) 
> and also at other assorted sources of help on the web but this problem 
> eludes me.
>
>
> I'm trying to model the following situation in a standalone Ruby 
> application using Sequel (both are new to me) –
>
>
> There are *customers*, who receive *services*. There are various fields 
> associated with the *service provision*, like actual and estimated costs 
> and so on. It makes sense to join the *customers *and *services* tables 
> through *service_provisions* and to keep these values in the 
> service_provisions table.
>
>
> My problem is that I would like to access the 'complete' service provision 
> information from one of my models easily, that is the one that includes 
> information from both the *services* and *service_provisions* tables.
>
>
> For example I currently have:
>
>
> Customer.first.services.first 
>
>  => #<Service @values={:id=>4, :service_id=>"window_cleaning", 
> :service_name=>"Communal Window Cleaning", :display_order=>400, 
> :category=>"block", :current=>true}> 
>
>
> Customer.first.service_provisions.first
>
> #<ServiceProvision @values={:id=>2, :customer_id=>1, :service_id=>4, 
> :actual=>10.0, :last_year_estimate=>9.0, :estimate=>11.0}> 
>
>
> And I don't know how to set it up so I could do something like
>
> Customer.first.service_provisions.first.service_name
>
> Customer.first.service_provisions.first.last_year_estimate
>
>
> or
>
>
> Customer.first.services.first.service_name
>
> Customer.first.services.first.last_year_estimate
>
>
> whichever makes more sense (I like the …provision option more).
>
>
> It is as if I want to join the *Service* and *Service Provision *data 
> together in some way? 
>
>
>
> My models are:
>
>   
>
> class Customer < Sequel::Model
>
>   one_to_many :service_provisions
>
  many_to_many :services, :join_table => :service_provisions
>

Your issue is that many_to_many by default does not select columns from the 
join table.  You can do so manually via the :select option:

  many_to_many :services, :join_table => :service_provisions, 
:select=>[:services_provisions.*, :services.*] 

That will allow you to do:

  Customer.first.services.first[:service_name]

Note that you need to use .[:column] instead of .column for columns in the 
join table, since methods are only created for columns in the model's 
table.  You can do:

  class Service
    def_column_accessor :service_name
  end

To create such methods manually, so that you can use .column.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sequel-talk/-/DDRce4p2aqgJ.
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