Question

My code is below

class City
  include DataMapper::Resource

  has n, :forums

  property :id,           Serial
  property :name,         String
  property :parent_state, String
  property :url,          String,  :length => 255
end

class Category
  include DataMapper::Resource

  has n, :forums

  property :id,           Serial
  property :name,         String
  property :url,          String,  :length => 255 
end

class Forum
  include DataMapper::Resource

  belongs_to :city
  belongs_to :category
  has n,     :posts

  property :id,           Serial
  property :rss,          String,  :length => 255
end

class Post
  include DataMapper::Resource

  belongs_to :forum

  property :id,           Serial
  property :title,        String,  :length => 255
  property :date,         Date
  property :time,         Time
  property :body,         Text
  property :url,          String,  :length => 255
  property :email,        String,  :length => 255

end

I can create a new City easily... (this is inside a loop that I don't think you really care to see):

City.create(:parent_state => state, :name => citylink.content, :url => citylink.get_attribute('href'))

but for the life of me I can't figure out how I create a new Forum (all Forum has is an RSS property). I've tried writing it 100 different ways and it either errors out or it just doesn't write to the database, i'm assuming because no association is given so it refuses to write it.

I've read through DM tutorials and writeups quite a lot and I still dont know what I would do.

Any help greatly appreciated!

This was my latest stupid sample test.. probably way off...

city = City.get(:name => cityname)
Forum.create(:city => city, :rss => "this works now")
Was it helpful?

Solution

It should just be:

forum = city.forums.create(:rss => "whatever")

If this doesn't work, try inspecting the errors for signs of anything obvious you've overlooked:

forum.errors.full_messages

(Assuming you have dm-validations included)

EDIT | By the way, this is not valid:

city = City.get(:name => cityname)

You probably want:

city = City.first(:name => cityname)

or

cities = City.all(:name => cityname)

When you use .get, you can only pass the primary key, like this:

city = City.get(1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top