0

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.

4
  • Wild guess: you've used attr_accessor in PortfolioFile Commented May 7, 2015 at 7:58
  • show your portfolio_file model Commented May 7, 2015 at 7:59
  • Yes I have to as I need to crop the attachment which requires attr_accessor. Can you please provide the solution. Commented May 7, 2015 at 8:00
  • @Yule. I have added portfolio_file model above. Please check. Commented May 7, 2015 at 8:05

4 Answers 4

2

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for it. I have changed that but its still not working.
Thanks for your response. I have added form code above. Please check.
1

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

1 Comment

So this answer worked, yet you accepted another answer? I'm confused.
1

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

2 Comments

I have permitted portfolio_id in params. Added code for that above.
Just added schema code for both table above. Please check.
0

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

1 Comment

Thanks for your reply. But I tried removing order from has_many relation but its still not working. I have pasted the code above. Please check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.