I have a question on example given in book Rails AntiPatterns by Chad Pytel

21 views
Skip to first unread message

Ming Ming

unread,
Jan 11, 2017, 7:18:10 PM1/11/17
to Singapore Ruby Brigade
Hi fellow rubyists,
  I been studying book Rails AntiPatterns by Chad Pytel.
And have been stumped by an example in book; the 2 related classes of which are reproduced below ...

class User < ActiveRecord::Base
  has_many :referral_types # this is Line_1
end

class Referral < ActiveRecord::Base
  has_and_belongs_to_many :users # this is Line_2
end

My questions are:
1) Is there a typo in Line_1? as in it should read "has_many :referrals" instead?
2) since Line_2 is a "has_and_belongs_to_many ", thus shouldn't Line_1 be also "has_and_belongs_to_many"? Cos my own understanding is that "has_and_belongs_to_many" should be a 2-way traffic between the 2 models involved.

Would appreciate if somebody could help answer my questions above ...

Winston Teo

unread,
Jan 11, 2017, 7:31:55 PM1/11/17
to singap...@googlegroups.com
Answering solely based on your question, without any context from the book.. 

Are these the only two classes? Is there actually a ReferralType class? If not, it does seem like a mistake. Does the book have an online errata page? 

Your understanding of 2) is correct too. For habtm, it's usually two ways. 

W



--


You received this message because you are subscribed to the Google Groups "Singapore Ruby Brigade" group.


To unsubscribe from this group and stop receiving emails from it, send an email to singapore-rb...@googlegroups.com.


To post to this group, send email to singap...@googlegroups.com.


Visit this group at https://groups.google.com/group/singapore-rb.


For more options, visit https://groups.google.com/d/optout.


Ming Ming

unread,
Jan 12, 2017, 7:54:16 AM1/12/17
to Singapore Ruby Brigade
yeo! tks for the reply ... now that you have mentioned it, a ReferralType class does sound more likely... i think your guess is correct.
As to "Does the book have an online errata page?", book doesn't.

On habtm, tks for the affirmation ... helps to know that i am on the right track.
Ruby/Rails newbie.

Paul Gallagher

unread,
Jan 17, 2017, 8:43:56 PM1/17/17
to Singapore Ruby Brigade
Hi Ming Ming,

You are obviously reading closely .. good catch;-)

At best it's "incomplete". I think in that section the author intends to give a sense of alternatives before presenting the solution in question. And I guess it does give you enough info to know the "type" of solution he means even though it is a broken example.

I believe you were basically right first off. For that code snippet to make a complete example:
*  referrals/referral_types => pick on and use in both places
* mirrored habtm in each model
i.e. yes this looks like a cut and paste error!

One way to look at stuff like this is just to map out the database. If we take the book example literally, this is what we get:

create_table :users do |t|
end

create_table :referrals do |t|
end

create_table :referral_types do |t|
  t.integer :user_id
end

create_table :referrals_users, id: false do |t|
  t.integer :referral_id
  t.integer :user_id
end

Now that is obvioulsy an incomplete solution to what the author was discussing. But if you were given that IRL, I guess it is possible to bodge it to meet the requirement. but it needs more tables and/or columns to do so.

But the fix/solution you were suggesting I think is:

class User < ActiveRecord::Base
  has_and_belongs_to_many :referral_types # this is Line_1
end

class ReferralType < ActiveRecord::Base
  has_and_belongs_to_many :users # this is Line_2
end

and table structure thus:

create_table :users do |t|
end

create_table :referral_types do |t|
end

create_table :referral_types_users, id: false do |t|
  t.integer :referral_type_id
  t.integer :user_id
end

and yes, that seems to do everything intended!

Cheers,
Paul
(still lurking in this list after .... years!)



To unsubscribe from this group and stop receiving emails from it, send an email to singapore-rb+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages