Thanks Jeremy, appreciate the help! Turns out I was unable to reproduce the error as I indicated it, so I apologize, it's likely I mis-posted. I'm still working through this though, so I will send additional info along if indeed I manage to reproduce it.
On Tuesday, May 29, 2012 4:30:36 PM UTC-5, Jeremy Evans wrote: > > On Tuesday, May 29, 2012 11:29:51 AM UTC-7, Miko wrote: >> >> Hi all, >> >> I'm new to Sequel and am trying to solve the case in a many-to-many >> relationship where the join table has a third column that is used to define >> relationship 'type'. A common example might be to define the role of an >> account related to some "thing". >> >> Here's an example to get started: >> >> class Account < Sequel::Model >> >> set schema do >> >> primary_key :id >> >> end >> >> >> >> one_to_many :accounts_posts >> >> many_to_many :posts, :through => :accounts_posts >> >> end >> >> >> class Post < Sequel::Model >> >> set schema do >> >> primary_key :id >> >> end >> >> >> >> one_to_many :accounts_posts >> >> many_to_many :accounts, :through => :accounts_posts >> >> end >> >> >> >> class AccountsPosts < Sequel::Model >> >> *# Fields: post_id, account_id*, *role_id* >> >> set schema do >> >> foreign_key :account_id, :accounts >> >> foreign_key :post_id, :posts >> >> integer :role_id, :null => false, :default => 1 # assume 1 is 'Owner' >> role, doesn't really matter though >> >> primary_key [:account_id, :post_id, :role_id] >> >> end >> >> >> >> one_to_many :accounts >> >> one_to_many :posts >> >> end >> >> >> I realize defining the AccountsThings class is unnecessary when it's a >> simple many_to_many. However, because multiple Account objects can be >> associated with a single Post, each with one or more roles (e.g. Owner, >> Observer, Editor, etc), I get "role_id is a restricted primary key" error >> messages if I try to do the following: >> > > You probably want to do: > > AccountsPosts.unrestrict_primary_key > > By default, primary keys are restricted, so you can't mass assign to them, > which is what causes that error message. > > For your case, where you can have multiple entries in the join table for a > given account/post combination, I think having a separate join model makes > sense. > > >> >> ats = AccountsPosts.new(:role_id => Role::OWNER) >> >> ats.account = @account >> >> ats.post = @post >> >> ats.save #=> error here >> >> >> > It's odd you get the error there. I would expect the error on the first > line (the call to new), since that's what does the mass assignment. If you > get the error on the save, it would be helpful if you could post the full > backtrace. > > > >> Wondering if anyone has solved this type of relationship before and could >> offer some pointers? I'm hopeful I don't even need the AccountsPosts class >> and can use the Sequel DSL to achieve my goal in a way that I'm not >> familiar. >> >> > Well, you don't necessarily need the AccountsPosts class. But given what > you are trying to do, I would recommend keeping it. > > 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/-/30LSdSJdaLEJ. 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.
