I see.  Thank you Jeremy.

In case someone else is having the similar problem.  Here is what I did.

First, the original schema.

DB.create_table(:iex_records) do
  primary_key :id
  char :source, :size => 10 #unique
end

DB.create_table(:eligibility_data) do
  primary_key :id
  char :source, :size => 10 #not unique
end

In order for associations to work, here is how I define my models:

class EligibilityDatum < Sequel::Model(DB_SQLITE)
  many_to_one :iex_record
end

class IexRecord < Sequel::Model(DB_SQLITE)
  one_to_many *:eligibility_data*, :key => *:source*, :*primary_key => 
:source*
end

pp @iex_record.eligibility_data_dataset
#<Sequel::SQLite::Dataset: "SELECT * FROM `medicare_eligibility_data` WHERE 
(`*eligibility_data*`.`*source*` = '*1012293073*')">

Note: no options needed for many_to_one.

:key = define which column to use in the associated table (ie: column 
"source" in table "eligibility_data")
  if :key is not defined, it will use the default assumption "#{table}_id" 
(ie: iex_record_id) instead

:primary_key = define the value to use for filtering (ie: 
@iex_record.source is '1012293073')
  if :primary_key is not defined, then it will use the default primary key 
(which is defined in the schema) value of that record (in my case, 
@iex_record.id, or 3073) .

Again, thank you Jeremy.


On Wednesday, June 6, 2012 12:29:13 PM UTC-7, Jeremy Evans wrote:
>
> On Wednesday, June 6, 2012 12:09:16 PM UTC-7, lkfken wrote:
>>
>> Hi,
>>
>> I have 2 models as follows:
>>
>> class IexRecord < Sequel::Model(DB_SQLITE)
>>   one_to_many :eligibility_data, :key => :source
>>   #Note: the original primary in the table was :id, but I set it to 
>> :source in other to get the Many-to-One association filter to work.
>>   set_primary_key :source
>>
>
> First, for problems like this, always post your database schema, as it 
> makes it much easier to diagnose the issue.
>
> You probably don't want to override the primary key manually.  If you 
> really do want to override it manually, you should override it before 
> defining the association, not after.  With the way you did things, at the 
> time the association is defined, it looks for the primary key, which would 
> be :id, and caches it.
>
> If you just want to change which primary key is used for that association, 
> without changing the model's actual primary key, use the :primary_key 
> association option. 
>
> 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/-/0YA2Cez-WXAJ.
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