On Friday, March 1, 2019 at 3:37:16 PM UTC-8, Nick Appelmans wrote:
>
> Hello Sequel developers.
>
> I have three models; Member, Auth_user and Log. A log entry will be made 
> by an Auth_user about a Member. The Auth_user is a member with special 
> privileges (password, etc). See the model and postgresql table descriptions 
> below.
>
> For every log entry, I am trying to identify the member the log was 
> written about and the member (who is the auth_user) who created the log so 
> that I can display the logged information along with the names of 
> individuals involved. I can do it but I'm sure there's a better way. (I 
> also would like to retrieve the relevant rows by starting with a Member or 
> an Auth_user). I would appreciate any suggestions. Here's how I currently 
> get the information...
>
> Start with a log entry
>
> > @log, @mbr, @au = Log, Member, Auth_user
>

Kind of odd to assign a model class to an instance variable, but we can run 
with it.
 

> given a log say, with id = 38 
>

> To find the name of the member that the log was recording the transaction 
> for...
>
> > mbr_name = @mbr[@log[38].values[:mbr_id]].values[:fname]
>

Probably best to use model accessors and the association methods (after 
fixing the association as described below):

  log = @log[38]
  mbr_name = log.member.fname 
 

> To find the name of the Auth_user that created the log...
>
> > au_name = @mbr[@au[@log[38][:a_user_id]].values[:mbr_id]].values[:fname]
>

Same:

  au_name = log.auth_user.member.fname
 

> Do I need to explicitly set up an association other than what I've done 
> below? Also, I'm thinking that I may have made a mistake in setting up the 
> associations given that in the association_basics.rdoc the recommendation 
> for a one-to-one relationship is...
>

The one_to_one call in Auth_user should be a many_to_one call, as the 
mbr_id column is a foreign key to members in the auth_users table.  And the 
one_to_one call in Member needs the :key and :class options set.  The 
one_to_many associations need to be plural and have the :key options set:

  Auth_user.many_to_one :member, key: :mbr_id
  Auth_user.one_to_many :logs, key: :a_user_id
  Member.one_to_one :auth_user, class: :Auth_user, key: :mbr_id
  Member.one_to_many :logs, key: :mbr_id

You may want to consider using AuthUser instead of Auth_user, as Auth_user 
goes against standard model naming style.
 

>
> "If you want to setup a 1-1 relationship between two models, where the 
> foreign key in one table references the associated table directly, you have 
> to use many_to_one in one model, and one_to_one in the other model. How do 
> you know which to use in which model?
>
> The simplest way to remember is that the model whose table has the foreign 
> key uses many_to_one, and the other model uses one_to_one:"
>
>
> I thought that since an auth_user is also a member and not all members are 
> auth_users, that would be one-to-one association set up on both sides. Am I 
> mistaken.
>

In Sequel, even if the database relationship is one-to-one, you don't use 
one_to_one on both sides. On the model with the table with the foreign key, 
you would use many_to_one.  For the model with the foreign key, the code 
for a one-to-one relationship is basically the same as a many-to-one, and 
quite different from the code for a one-to-one relationship with the 
foreign key in the other table (which is similar to the code for a 
one-to-many relationship).  I think the documentation you quoted (and the 
ASCII art diagram that goes with it) does a decent job explaining it, but 
I'm definitely open to pull requests to make it more clear.

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.

Reply via email to