Ruby on Rails Saturday, May 23, 2015

Okay... I am really trying to be super vigilant to best Ruby/Ruby on Rails practices here... the whole n + 1 matter particularly

I have a parent table called Advertiser:

This is how it looks on the database:

CREATE TABLE advertisers (
  advertiser_id varchar(40) NOT NULL,
  title varchar(50) NOT NULL,
  category_id int(3) NOT NULL DEFAULT '99',
  category_other varchar(50) DEFAULT NULL,
  description varchar(50) DEFAULT NULL,
  advertiser_url varchar(50) NOT NULL,
  advertiser_email varchar(50) NOT NULL,
  approved int(3) NOT NULL,
  date_created datetime NOT NULL,
  date_updated datetime DEFAULT NULL,
  date_deleted datetime DEFAULT NULL,
  is_active varchar(3) NOT NULL,
  is_activated int(3) DEFAULT NULL,
  remote_ip varchar(50) DEFAULT NULL,
  PRIMARY KEY (advertiser_id),
  KEY `name` (title),
  KEY date_created (date_created)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

This is its model: It is listed as advertiser.rb


class Advertiser < ActiveRecord::Base
 
  require 'uri'
 
  self.primary_key = 'advertiser_id'
  attr_accessor :item_image_upload
  has_one :item_image
  has_one :category
 
  accepts_nested_attributes_for :item_image
  before_create :get_advertiser_primary_key_value
  before_create :url_valid
     
  validates_uniqueness_of :title,:if => :title, :case_sensitive => true, :message =>  " already exists."
  validates_uniqueness_of :advertiser_email,:if => :advertiser_email, :case_sensitive => false, :message =>  " already exists"
  validates_uniqueness_of :advertiser_url,:if => :advertiser_url, :case_sensitive => false, :message =>  " already exists"
 
 
 def url_valid
   uri = URI.parse(self.advertiser_url)
   if uri.kind_of?(URI::HTTP)
     return true
   else
     self.errors.add(:advertiser_url, ": URL Link improperly formatted: http://")
     return false
  end
 
 end

protected

  def get_advertiser_primary_key_value
     self.advertiser_id = get_random
  end

  def get_random
    length = 36
    characters = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
    @id = SecureRandom.random_bytes(length).each_char.map do |char|
      characters[(char.ord % characters.length)]
    end.join
    @id
  end
 
 end

And the parent table has a look up table source called categories:

Here is how it looks on the database:

CREATE TABLE categories (
  category_id int(3) NOT NULL DEFAULT '99',
  category_type varchar(50) NOT NULL,
  KEY category_type_index (category_type)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


Categories exists as category.rb, and this is the model:

class Category < ActiveRecord::Base
 
 self.primary_key = 'category_id'
 
  belongs_to :advertiser
  belongs_to :borrower
  belongs_to :lender

end

 
Here is the Advertiser controller:

class AdvertiserController < ApplicationController
  def new
    @advertiser = Advertiser.new

    respond_to do |f|
      f.html
    end
...
  end

....

end

And now here is the new.html.erb view with irrelevant stuff excluded....


<%= form_for @advertiser, :url=> {:controller=>"advertiser", :action => "create"},:html=>   {:class=>"advertiser", :multipart=>true} do |nf| %>


<% unless  @advertiser.errors.empty? %>
<%= render :partial => 'home/check_for_errors', :locals => {:data_source => @advertiser}  %>
<% end %>


<label>Advertisement Category:</label>
    THIS IS WHERE I AM HAVING Problems...           
    Tried fields_for
    Every variety of Rails select
    <%= nf.select :category_id, Category.all.collect {|p| [ p.category_type, p.category_id ] }  %>  ### This doesn't work
   
    I am really committed to using best practices.  What an I doing wrong?  I am not clear as to model requirement regarding look up values that are user nonchangeable... The whole notion, for example, has_one with regard to a look up is not clear to me.


<% end %>


Thanks, Liz McGurty




--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/291fb169-effa-4049-82d4-8b3d6d534706%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment