Akhil Latta
Akhil Latta

Reputation: 558

Rails 4 always adding null value for column

I have a column named portfolio_id in portfolio_files table. portfolio_files table belongs_to portfolio table. Now when I create a new record for portfolio_files table, it always create record with null portfolio_id. I have even tried providing static value for portfolio_id but its still not working.

Here is my controller code:

def new
 @temp = PortfolioFile.new
 @temp.attachment = temp_params['attachment']
 @temp.order = 10
 @temp.portfolio_id = 47
 @temp.save
 respond_to do |format|
  format.js { render :locals => { :temp => @temp, :form_id =>temp_params['form_id']} }
 end
end

private

def temp_params
 params.require(:portfolio_file).permit(:attachment, :form_id, :id,:crop_x,:crop_y,:crop_w,:crop_h,:category_id,:order,:color,:text,:action_type, :portfolio_id)
end

Portfolio Model:

class Portfolio < ActiveRecord::Base
 has_many :portfolio_detail
 has_many :portfolio_file, -> { order 'portfolio_files.order' }
 belongs_to :category
 belongs_to :user
end

Schema

create_table "portfolio_files", force: :cascade do |t|
 t.string   "attachment_file_name"
 t.string   "attachment_content_type"
 t.integer  "attachment_file_size"
 t.datetime "attachment_updated_at"
 t.integer  "order", limit: 2, default: 0
 t.integer  "portfolio_id", null: false
end

create_table "portfolios", force: :cascade do |t|
 t.integer  "user_id"
 t.integer  "category_id"
 t.integer  "order",       limit: 2
 t.datetime "created_at"
 t.datetime "updated_at"
 t.integer  "form_id",     limit: 2
 t.integer  "viewcount",             default: 0
end

PortfolioFile Model:

class PortfolioFile < ActiveRecord::Base
 belongs_to :portfolio

 has_attached_file :attachment, :styles => lambda { |attachment| {:medium =>  attachment.instance.video? ? { :geometry => "236x236>", :format => 'jpg', :time => 10 } : { :geometry => "236x236>" }, :thumb => attachment.instance.video? ? { :geometry => "150x150>", :format => 'jpg', :time => 10 } : { :geometry => "150x150>" },:large => attachment.instance.video? ? { :geometry => "1000x700", :format => 'jpg', :time => 10 } : { :geometry => "1000x700" }} },:processors => lambda { |a| a.video? ? [ :transcoder ] : [ :cropper ] }

before_post_process :is_audio?

def is_audio?
    return false if [ 'audio/mp3','audio/mpeg'].include?(attachment.content_type) or attachment.instance.action_type.blank?
end

def video?
 [ 'application/x-mp4',
  'video/mpeg',
  'video/quicktime',
  'video/x-flv',
  'flv-application/octet-stream',
  'video/3gpp',
  'video/3gpp2',
  'video/3gpp-tt',
  'video/BMPEG',
  'video/JPEG',
  'video/mp4',
  'video/MP4V-ES',
  'video/MPV',
  'video/mpeg4',
  'video/mpeg4-generic'].include?(attachment.content_type)
 end

 attr_accessor :form_id, :crop_x, :crop_y, :crop_w, :crop_h, :color, :text, :portfolio_id, :action_type

 validates_attachment_content_type :attachment, :content_type => /.*/

 def cropping?
  !crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
 end

 def attachment_geometry(style = :original)
  @geometry ||= {}
  @geometry[style] ||= Paperclip::Geometry.from_file(attachment.path(style))
 end

private
def reprocess_attachment
 attachment.reprocess!
end

end

Form:

<form method="post" accept-charset="UTF-8" action="/en/portfolio_files/new" enctype="multipart/form-data" class="new_portfolio_file" id="portfolio_work_form3">
 <input type="hidden" value="✓" name="utf8"><input type="hidden" value="fOzK0WL5f00S+QBGD9NAwpHrKGOhm5tUZojDHFXdUfH4ZEIf0QL9sERtI4RdYVRx6EeXS9xYCSL+Lsx3NdZm3A==" name="authenticity_token">
 <input type="file" id="portfolio_file_attachment" multiple="multiple" name="portfolio_file[attachment]">
 <input type="hidden" id="portfolio_file_form_id" name="portfolio_file[form_id]" value="portfolio_work_form3">
 <input type="hidden" id="portfolio_file_portfolio_id" name="portfolio_file[portfolio_id]" value="48">
 <input type="hidden" id="portfolio_file_edit_portfolio" name="portfolio_file[edit_portfolio]" value="48">
 <span class="port_form_error" id="portfolio_work_form3_error"></span
 </form>

I am submitting this form with jQuery file uploader.

Please suggest. Thanks in advance.

Upvotes: 0

Views: 270

Answers (4)

Yule
Yule

Reputation: 9764

You have portfolio_id as an attr_accessor in your portfolio file. You don't need this. This will set a 'virtual' attribute that is not persisted, overriding active-record's 'magic'. AR adds a getter and setter for database fields, meaning you don't need to manually specify attr_accessor

Upvotes: 1

abbott567
abbott567

Reputation: 862

I think there might be an issue with your ordering... You usually need to assert how you want to order the collection

has_many :portfolio_files, -> { order 'created_at desc' }

Usually you would state an attribute to order by

Upvotes: 0

Gaurav Gupta
Gaurav Gupta

Reputation: 1191

Check you have permitted 'portfolio_id' in params or not (rails strong attribute)

Upvotes: 1

Rajdeep Singh
Rajdeep Singh

Reputation: 17834

You have not followed convention over configuration, you need to use pluralized model name with has_many, instead of portfolio_file write portfolio_files

has_many :portfolio_files, -> { order 'portfolio_files.order' }

Hope this helps!

Upvotes: 2

Related Questions