This project is archived and is in readonly mode.

#6165 ✓invalid
Alexander Zubkov

Foreign key is not set on nested record

Reported by Alexander Zubkov | December 14th, 2010 @ 07:51 PM

I have two nested resources:

resources :photo_albums do
  resources :photos
end

These two models are correctly linked with has_many and belongs_to.

When adding a photo I get this error:

Mysql2::Error: Column 'photo_album_id' cannot be null

The workaround is to add this line to my controller:

@photo.photo_album_id = params[:photo_album_id]

But in rails 2.3 it was done automatically. Another application has broken only after update to Rails 3.0.3, I did not change anything else.

Comments and changes to this ticket

  • Alexander Zubkov

    Alexander Zubkov January 26th, 2011 @ 08:26 AM

    Don't you agree it is a bug? Any comments?

  • Rohit Arondekar

    Rohit Arondekar January 26th, 2011 @ 09:57 AM

    • Importance changed from “” to “Low”

    Alexander, could you please paste the relevant code of the models as well for completeness. Also are you using @photo.photo_albums.build(params[:photo_album]) — since #create & #new will not fill the photo_album_id automatically while #build will.

  • Alexander Zubkov

    Alexander Zubkov January 26th, 2011 @ 11:33 AM

    class PhotoAlbum < ActiveRecord::Base
    attr_accessible :name, :description, :visible

    has_many :photos, :dependent => :destroy

    validates_presence_of :name

    scope :visible, lambda { |admin| where(:visible => true) unless admin }

    cattr_reader :per_page @@per_page = 6 end

    class Photo < ActiveRecord::Base
    attr_accessible :name, :position, :photo, :visible, :photo_album_id

    belongs_to :photo_album

    has_attached_file :photo, :styles => {

      :cover => '100x100#',
      :thumb => '150x150#',
      :original => '980x720>' }
    

    before_post_process { translit_paperclip_file_name self.photo }

    scope :visible, lambda { |admin| where(:visible => true) unless admin }

    before_save :set_position, :on => :create

    protected def set_position

    self.position = 1 + Photo.where(:photo_album_id => self.photo_album_id).maximum(:position).to_i
    

    end end

    Well, I use cancan with load_resource, so I skip that line:
    @photo.photo_albums.build(params[:photo_album]) in my controller. I don't know, if cancan is using #new or #build, but probably it is a cancan bug, I will check and answer later. Thank you.

  • Rohit Arondekar

    Rohit Arondekar January 27th, 2011 @ 06:12 AM

    I have not used cancan before but just browsing it's source code on Github I found https://github.com/ryanb/cancan/blob/master/lib/cancan/controller_a...:

      # [:+new+]
      #   Specify which actions are new resource actions in addition to :+new+ and :+create+.
      #   Pass an action name into here if you would like to build a new resource instead of
      #   fetch one.
      #
      #     load_resource :new => :build
    

    Are you using that? If not it might be what you are looking for.

  • Alexander Zubkov

    Alexander Zubkov January 27th, 2011 @ 11:12 AM

    Yes, you're right. #new does not set foreign key, even on Rails 2.3. So probably cancan behavior has changed, so I had to add that line. Thank you for explanations. It's not Rails bug, let's close this ticket.

  • Rohit Arondekar

    Rohit Arondekar January 27th, 2011 @ 12:24 PM

    • State changed from “new” to “invalid”

    Cheers :)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages