Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Association not working #149

Open
iamdriz opened this issue Apr 28, 2017 · 3 comments
Open

Association not working #149

iamdriz opened this issue Apr 28, 2017 · 3 comments

Comments

@iamdriz
Copy link

iamdriz commented Apr 28, 2017

I have the following two models:

class ActionType < ActiveHash::Base
  has_many :action_histories
  self.data = [
      {:id => 1, :name => 'call_started', :friendly_name => 'Call Started'},
      {:id => 2, :name => 'call_ended', :friendly_name => 'Call Ended'},
      {:id => 3, :name => 'email_sent', :friendly_name => 'Email Sent'},
      {:id => 4, :name => 'email_received', :friendly_name => 'Email Received'},
      {:id => 5, :name => 'note', :friendly_name => 'Note'}
  ]
end

class ActionHistory < ApplicationRecord
  belongs_to :action_type
end

And my action_histories table has action_type_id column for the relationship.
However when I try and access ActionHistory.all.first.action_type.name I get the error that action_type is not defined.

I've tried using extend ActiveHash::Associations::ActiveRecordExtensions and include ActiveHash::Associations but that doesn't get it working either...

I'm using latest version of Gem and Rails 5.0.1

@kbrock
Copy link
Collaborator

kbrock commented Apr 29, 2017

Have you tested this with belongs_to_active_hash?

possibly:

class ActionHistory < ApplicationRecord
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to_active_hash :action_type
end

Some people have all active record objects extending ActiveHash::Associations::ActiveRecordExtensions but that is a style decision

@iamdriz
Copy link
Author

iamdriz commented May 2, 2017

@kbrock What would I have in the ActionType class? As having the has_many definition causes the error: undefined method 'has_many' for ActionType:Class. Adding the above code you posted doesn't work :/ so looks like the association isn't working.

@kbrock
Copy link
Collaborator

kbrock commented May 3, 2017

It sounded like you already tried this
According to the specs, it looks like this should be working:

class ActionHistory < ApplicationRecord
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to_active_hash :action_type
end

class ActionType < ActiveHash::Base
  include ActiveHash::Associations # this looks like the one
  has_many :action_histories
  self.data = [
      {:id => 1, :name => 'call_started', :friendly_name => 'Call Started'},
      {:id => 2, :name => 'call_ended', :friendly_name => 'Call Ended'},
      {:id => 3, :name => 'email_sent', :friendly_name => 'Email Sent'},
      {:id => 4, :name => 'email_received', :friendly_name => 'Email Received'},
      {:id => 5, :name => 'note', :friendly_name => 'Note'}
  ]
end

Did you try something like this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants