On Thursday, June 22, 2017 at 9:19:19 PM UTC-7, Anonymous Testing wrote:
>
> Tutaj wprowadź kod...
>
> Hello guys!
> I have very weird problem with many to many associations. Before any info
> I must notice that I can't change table structure cause I rewriting app and
> DB must looks the same.
>
> First model
> class User < Sequel::Model
> many_to_many :devices, left_key: :token, right_key: :user, join_table:
> :users_devices
> end
>
> Second model
> class Device < Sequel::Model
> many_to_many :users, left_key: :hash, right_key: :device, join_table:
> :users_devices
> end
>
> Joining class
> class UsersDevice < Sequel::Model
> many_to_one :user
> many_to_one :device
> end
>
> Migration
> create_table :devices do
> primary_key :id, type: Integer
> column :device_id, String
> column :hash, String
> end
>
> create_table :users do
> primary_key :id, type: Integer
> column :token, String
> end
>
> create_table :users_devices do
> primary_key :id, type: Integer
> column :device, String, null: false
> column :user, String, null: false
> end
>
>
> When I try to get all users from device I see in logs:
> Sequel::Postgres::Database (2.2ms) SELECT "users".* FROM "users" INNER
> JOIN "users_devices" ON ("users_devices"."device" = "users"."id") WHERE
> ("users_devices"."hash" = 142)
>
> Of course, it doesn't work cause I try map device with id instead of
> device with hash...
> I do something wrong?
>
You probably want something like:
Device.many_to_many :users, left_primary_key: :hash, left_key: :device,
right_key: :user, :right_primary_key :token, join_table: :users_devices
The documentation does explain what each option does, you should probably
review it:
http://sequel.jeremyevans.net/rdoc/files/doc/association_basics_rdoc.html#label-3Aleft_key+-5Bmany_to_many-2C+one_through_one-5D
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.