Skip to content

Instantly share code, notes, and snippets.

@repertoire
Created January 18, 2011 23:23
Show Gist options
  • Save repertoire/785371 to your computer and use it in GitHub Desktop.
Save repertoire/785371 to your computer and use it in GitHub Desktop.
class Video < ActiveRecord::Base
has_many :video_locations
has_many :locations, :through => :video_locations
accepts_nested_attributes_for :locations
has_many :video_people
has_many :people, :through => :video_people
attr_accessible :title, :date, :url
validates_presence_of :title, :date, :url
validate :validates_presence_of_locations
def validates_presence_of_locations
errors.add(:locations, "must have at least one location") if
locations.length < 1
end
end
# Works:
irb(main):001:0> @video = Video.new(:title => 'test', :url => 'test', :date => DateTime.now)
=> #<Video id: nil, title: "test", date: "2011-01-18 23:16:48", url: "test", created_at: nil, updated_at: nil>
irb(main):002:0> @video.locations_attributes = [ { :lat => 50, :lng => 13 }, { :lat => 12, :lng => 15 } ]
=> [{:lat=>50, :lng=>13}, {:lat=>12, :lng=>15}]
irb(main):003:0> @video.valid?
=> true
irb(main):005:0> @video.save
=> true
irb(main):006:0>
# Whereas, I get this in the log when testing app in browser:
<snip>
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WqnRHIpk39nMnNiav13qQBMaPM6rbBnf/iLHh63BKTQ=", "video"=>{"title"=>"Test!", "date(1i)"=>"2011", "date(2i)"=>"1", "date(3i)"=>"18", "date(4i)"=>"23", "date(5i)"=>"10", "locations_attributes"=>{"0"=>{"lat"=>"52.518091129872246", "lng"=>"13.406529008428958"}}, "url"=>"TEST!"}, "commit"=>"Create Video"}
<snip>
WARNING: Can't mass-assign protected attributes: locations_attributes
<snip>
# @*!$&#%??
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment