lucascaton/enumerate_it

Quick question on EnumerateIt

mrdinghy opened this issue · 8 comments

Greetings ... great gem but not clicking yet.
I've set this up and it seems to work fine as documented... except I cannot save it.

i have model

class EventType < EnumerateIt::Base
  associate_values(
      :phone   => [1, 'Phone'],
      :meeting  => [2, 'Meeting'],
      :conference    => [3, 'Conference'],
      :task => [4, 'Task'],
      :email => [4, 'Email']
  )
end 

and in Events:

class Event < ActiveRecord::Base
  extend EnumerateIt
  attr_accessor :event_type
  has_enumeration_for :event_type, :with => EventType
end

My event_type column in the DB in events is type: integer
When i manually enter data in the DB and try to list the records nothing dispalys for Event_type even though i can debug it and see the value in the attributes.

--- !ruby/object:Event
raw_attributes:
  id: '1'
  name: Start up Meeting
  event_start: '2015-03-02 03:52:00'
  event_end: '2015-03-02 15:52:00'
  manager_id: '1'
  backup_id: '1'
  location: meeting room 3
  created_at: '2015-03-02 03:53:57.160064'
  updated_at: '2015-03-02 03:53:57.160064'
  description: 
  eventable_id: 
  eventable_type: 
  event_type: '3'

I am thinking maybe I need to use another column type. What value should get saved?
:meeting or 2 ?

in the form the drop down works fine and passes the value to the console object and the request parameters

{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"Q5J84VkpYcr1nGSoJDmatOkXA2s2yvvcY322epuCLDJuFmzxPRrhQNNw4HANJPMSmD2rf5TMNHqypoBPmdNR4w==", "event"=>{"name"=>"Start up Meeting", "event_type"=>"4", "event_start(1i)"=>"2015", "event_start(2i)"=>"3", "event_start(3i)"=>"2", "event_start(4i)"=>"03", "event_start(5i)"=>"52", "event_end(1i)"=>"2015", "event_end(2i)"=>"3", "event_end(3i)"=>"2", "event_end(4i)"=>"15", "event_end(5i)"=>"52", "manager_id"=>"1", "backup_id"=>"1", "location"=>"meeting room 3"}, "commit"=>"Update Event", "controller"=>"events", "action"=>"update", "id"=>"1"}

but I get this error when it tries to update:

ActionView::MissingTemplate at /events/1
Missing template events/update, application/update with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/home/ims/railsdev/watan_0228/app/views" * "/home/ims/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.4.1/app/views"

any help would be appreciated
Thanks
Steve

Also i tried another model on this (different but same code) and when I update is get
enumeratethis

Hi,

If you have an enumeration like

class EventType
  associate_values (
    :meeting  => [2, 'Meeting'],
    :conference    => [3, 'Conference'],
    :task => [4, 'Task'],
    :email => [5, 'Email']
  )
end

The value of the "enumerated attribute" is going to be the integer number. You could have an enumeration defined like:

class EventType
  associate_values :conference, :task, :email
end

and in that case the value would be "conference", "task", etc. If you want to use the enumeration with an ActiveRecord model and persist it to the database, the column has to have the correct data type, but it can be a number or a string, depending on how you defined you enumeration.

Your first error is not related to the gem, it looks like you are missing a view file that maps to your update action, or you should be redirecting and you're not.

Also, when using EnumerateIt in conjunction with ActiveRecord, it will validate if the received value for the enumerated attribute exists in the enumeration. That's why you have the other error.

Thanks for the reply... yeah i understand that first error.

the event_type in the DB was integer.

I created another verison which uses a string:
Same thing... the form is submitted with the correct name=value showing up in the "puts params"

the param is not saved to the DB
I tried manually inputing "news" and ":news" in the field but it would not show up on Show.
enumerater:

The column in the DB is string "lead_source"

class LeadSource < EnumerateIt::Base
  associate_values :conference, :task, :email, :news
end

model applied to:

class Lead < ActiveRecord::Base
  extend EnumerateIt
 attr_accessor :lead_source
  has_enumeration_for :lead_source, :with => LeadSource

  has_many :opportunities, :through => :leads_opportunities
  has_many :leads_opportunities, :dependent => :destroy
  has_many :contacts
  has_many :events
end

i checked the permits they include the field

def lead_params
      params.require(:lead).permit(:name, :lead_source, :manager_id, :backup_id, :description)
    end

console:

{"utf8"=>"✓", "_method"=>"patch", "authenticity_token"=>"rr0ANA/5JU/YxhsAoH+83wYg8S2GA57Gcbe+TWFSz1mDORAka8qlxf4qn9iJYtV5dwpZOSQFUWCgbIh4YwOyiA==", "lead"=>{"name"=>"Funding for new School in Province", "lead_source"=>"news", "manager_id"=>"", "backup_id"=>"", "description"=>"sdfafsfsf"}, "commit"=>"Update Lead", "controller"=>"leads", "action"=>"update", "id"=>"1"}

Some questions:

  • Is the record being saved but the lead_source attribute is nil or is the record not being saved at all?

  • If you go to the rails console and try something like the following, does it work?

    lead = Lead.first
    lead.update_attributes lead_type: LeadType::TASK
    lead.reload
    lead.lead_type # should output "task"

I tried that ...when I updated i got:

2.0.0-p353 :012 > lead.update_attributes lead_source: LeadSource::TASK
   (0.3ms)  BEGIN
   (0.2ms)  COMMIT
 => true 

and this for the event_type (integer).

2.0.0-p353 :027 >   event.update_attributes event_type:1
   (0.4ms)  BEGIN
   (0.3ms)  COMMIT
 => true   

when i reload both they remain nil
If i manually enter ":task" or "1" in the POSTGRES table it will show up on the console when loaded and will show in debug output on browser but not display using <%= @event.event_type %>

ill try and load the gem on a fresh rails install

Basically .... if I follow the directions

add the gem to the Gemfile
bundle install
rails g enumerate_it:install
create event_type model
add extension to event model.

Is it required to add that code to the application.rb file as well????

thanks

This is what happens in a basic rails save attempt (saving an integer for event _type)
eventsave

I created an example Rails application showing how you can integrate EnumerateIt into your project.

https://github.com/cassiomarques/enumerate_it_rails_example