#772 ✓resolved
Carl Lerche

Associations don't work if the FK property is private / protected

Reported by Carl Lerche | January 15th, 2009 @ 01:42 PM

The following does not work:

Setting FKs as private is needed to protect them against mass assignment, but then association code does not work. See the following example.

#!/usr/bin/env ruby -KU
require "rubygems"
require "dm-core"

class User
  include DataMapper::Resource
  property :id,   Serial
  property :name, String
  has n, :comments

class Comment
  include DataMapper::Resource
  property :id,      Serial
  property :user_id, Integer, :accessor => :private
  property :body,    String
  belongs_to :user

DataMapper.setup(:default, "sqlite3::memory:")

user = User.create(:name => "Carl")
user.comments.create(:body => "Hello World")

comment = Comment.first
puts "#{comment.user.name} said: #{comment.body}"

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

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