one class, multiple associations

24 views
Skip to first unread message

Mark Sobkowicz

unread,
Jun 12, 2011, 8:09:12 PM6/12/11
to Hobo Users
I have models for User and Message. Users should be able to send messages with multiple Users as the recipients. Here are the associations:

User:

has_many :received_messages, :class => "Message", :through => :message_recipients, :accessible => true
has_many :message_recipients, :dependent => :destroy, :accessible => true, :foreign_key => 'recipient_id'
has_many :sent_messages, :class => "Message", :foreign_key => 'sender_id', :accessible => true

Message:

belongs_to :sender, :class => "User", :creator => true, :foreign_key => 'sender_id'
has_many :recipients, :class => "User", :through => :message_recipients, :accessible => true
has_many :message_recipients, :dependent => :destroy, :accessible => true

MessageRecipient: (join model)

belongs_to :message
belongs_to :recipient, :class => "User", :foreign_key => 'recipient_id'


I set this up, then ran hobo g migration (hobo 1.3 pre31) here is the beginning of the trace:

Hobo Command Line Interface 1.3.0.pre31
/Library/Ruby/Gems/1.8/gems/activesupport-3.0.7/lib/active_support/core_ext/hash/keys.rb:43:in `assert_valid_keys': Unknown key(s): class (ArgumentError)
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.7/lib/active_record/associations.rb:1774:in `create_belongs_to_reflection'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.7/lib/active_record/associations.rb:1220:in `belongs_to'
from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.7/lib/active_record/autosave_association.rb:137:in `belongs_to_without_hobo_permission_check'
from /Library/Ruby/Gems/1.8/gems/hobo-1.3.0.pre31/lib/hobo/model/permissions.rb:110:in `belongs_to_without_accessible'
from /Library/Ruby/Gems/1.8/gems/hobo-1.3.0.pre31/lib/hobo/model/accessible_associations.rb:125:in `belongs_to_without_creator_metadata'
from /Library/Ruby/Gems/1.8/gems/hobo-1.3.0.pre31/lib/hobo/model.rb:159:in `belongs_to_without_test_methods'
from /Library/Ruby/Gems/1.8/gems/hobo-1.3.0.pre31/lib/hobo/model.rb:163:in `belongs_to_without_field_declarations'
from /Library/Ruby/Gems/1.8/gems/hobo_fields-1.3.0.pre31/lib/hobo_fields/model.rb:93:in `belongs_to'
from /Users/mark/Documents/Hobo/class_management/app/models/message.rb:10


line 10 in message.rb is the belongs_to line.


Any ideas where I'm going wrong?

Mark Sobkowicz

unread,
Jun 12, 2011, 8:59:26 PM6/12/11
to Hobo Users
I got it to give the same error with a much simpler setup. In a new hobo app, I added a messages model. Then

just adding this to the Message model and trying to run the migration gives the same error:

belongs_to :sender, :class => "User"

Matt Jones

unread,
Jun 12, 2011, 9:14:21 PM6/12/11
to hobo...@googlegroups.com

On Jun 12, 2011, at 8:59 PM, Mark Sobkowicz wrote:

> I got it to give the same error with a much simpler setup. In a new hobo app, I added a messages model. Then
>
> just adding this to the Message model and trying to run the migration gives the same error:
>
> belongs_to :sender, :class => "User"


Perhaps because the ActiveRecord option is :class_name?

--Matt Jones

Mark Sobkowicz

unread,
Jun 13, 2011, 8:20:15 AM6/13/11
to hobo...@googlegroups.com
Yes!  That was the problem.   Now the form for messages doesn't work.   The associations are:

Message

  belongs_to :sender, :class_name => "User", :creator => true, :foreign_key => 'sender_id'
  has_many :recipients, :class_name => "User", :through => :message_recipients, :accessible => true, :foreign_key => 'recipient_id'
  has_many :message_recipients, :dependent => :destroy


User

  has_many :received_messages, :class_name => "Message", :through => :message_recipients  #possible I have to name class here?
  has_many :message_recipients, :dependent => :destroy,  :foreign_key => 'recipient_id'
  has_many :sent_messages, :class_name => "Message", :foreign_key => 'sender_id'

MessageRecipient

 belongs_to :message
  belongs_to :recipient, :class_name => "User", :foreign_key => 'recipient_id'
  
  
The generated form for messages looks like this:

<def tag="form" for="Message">
  <form merge param="default">
    <error-messages param/>
    <field-list fields="subject, body, recipients, sender" param/>
    <div param="actions">
      <submit label="#{ht 'message.actions.save', :default=>['Save']}" param/><or-cancel param="cancel"/>
    </div>
  </form>
</def>

But visiting /messages/new gives the error:

undefined method `klass' for nil:NilClass
app/views/taglibs/application.dryml:37:in `form__for_message'
app/views/taglibs/application.dryml:35:in `form__for_message'
app/views/taglibs/application.dryml:35:in `form__for_message'
app/views/taglibs/application.dryml:34:in `form__for_message'
app/views/taglibs/auto/rapid/pages.dryml:445:in `new_page__for_message'
app/views/taglibs/auto/rapid/pages.dryml:444:in `new_page__for_message'
app/views/taglibs/auto/rapid/pages.dryml:444:in `new_page__for_message'
app/views/taglibs/auto/rapid/pages.dryml:435:in `new_page__for_message'
app/views/taglibs/themes/clean/clean.dryml:2:in `page_without_a4e8f2708e51'
app/views/taglibs/themes/clean/clean.dryml:1:in `page_without_a4e8f2708e51'
app/views/taglibs/application.dryml:12:in `page_without_a46705120a36'
app/views/taglibs/application.dryml:11:in `page_without_a46705120a36'
app/views/taglibs/application.dryml:24:in `page_without_a0bbed2e9e5e'
app/views/taglibs/application.dryml:23:in `page_without_a0bbed2e9e5e'
app/views/taglibs/front_site.dryml:13:in `page'
app/views/taglibs/front_site.dryml:12:in `page'
app/views/taglibs/auto/rapid/pages.dryml:432:in `new_page__for_message'
app/views/taglibs/auto/rapid/pages.dryml:431:in `new_page__for_message'

The issue is definitely "recipients" in the field-list tag.   

Bob Sleys

unread,
Jun 13, 2011, 8:36:32 AM6/13/11
to hobo...@googlegroups.com
I think message_recipients needs to be part of the form because the relationship goes through it.

Mark Sobkowicz

unread,
Jun 13, 2011, 8:46:44 AM6/13/11
to hobo...@googlegroups.com
I don't think so.  I have working apps with has_many :through associations - the field-lists for the forms do not have the association model listed.   


On Jun 13, 2011, at 8:36 AM, Bob Sleys wrote:

I think message_recipients needs to be part of the form because the relationship goes through it.

-- 

Mark Sobkowicz

unread,
Jun 13, 2011, 9:13:29 AM6/13/11
to hobo...@googlegroups.com
Hmm.  This doesn't work on the "new" page

 <field-list fields="subject, body, recipients, sender" param/>

But this does work:

<field-list fields="subject, body, sender" param/>
<input:recipients />

What is also interesting is that the first method works fine on an "edit" view, but not on a "new" view, while the second way works fine for both.




On Jun 13, 2011, at 8:36 AM, Bob Sleys wrote:

I think message_recipients needs to be part of the form because the relationship goes through it.

--
You received this message because you are subscribed to the Google Groups "Hobo Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/hobousers/-/ojYvyP71MT0J.
To post to this group, send email to hobo...@googlegroups.com.
To unsubscribe from this group, send email to hobousers+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/hobousers?hl=en.

Reply all
Reply to author
Forward
0 new messages