Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Foreign key is not set on nested record

#6165

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.

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

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Alexander Zubkov
    Alexander Zubkov

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

    January 26th, 2011 @ 08:26 AM

  2. Rohit Arondekar
    Rohit Arondekar
    • 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.

    January 26th, 2011 @ 09:57 AM

  3. Alexander Zubkov
    Alexander Zubkov

    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.

    January 26th, 2011 @ 11:33 AM

  4. Rohit Arondekar
    Rohit Arondekar

    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.

    January 27th, 2011 @ 06:12 AM

  5. Alexander Zubkov
    Alexander Zubkov

    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.

    January 27th, 2011 @ 11:12 AM

  6. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to invalid

    Cheers :)

    January 27th, 2011 @ 12:24 PM