basaah asked:

How Create a hasone property object with Og

Tags: og model

see I have A model like this.

class User
  property :name,String
  property :pass, String
  property :email, String
  
  has_one :rooster, Rooster
  def initialize(name = nil,pass = nil,email=nil)
    @name = name
    @pass = pass.crypt('ab') if pass != nil
    @email = email
  end
  def to_s
    @name
  end  
end  

class Rooster
  property :nmr, String
  
  has_many :uren, Uur
  belongs_to :User, User
  def initialize(nmr = nil)
    @nmr = nmr
  end
end  
#class Uur left away

I can create A new user by doing

user = User.create(name,pass,email)
but then the user wants a rooster. so something like
user = User.create(name,pass,email)
user.create_rooster(nmr)

but that doesn't work if I do

rooster.create(nmr)
it is logical that the rooster doesn't know what user it haves (a blank user_oid) and vica versa.

How can I create a rooster as a property of user, that means that the _oid are correct.

also how can I find this when I have the user object

rooster = user.rooster
#or 
rooster = rooster.find_by_oid(user.rooster_oid)
I mean I just want the magical Og way for this.

also one more for how this goes for hasmany and manyto_many relationships.

I hope anyone can help me, cause I could find the answer myself

(2 attempts)

Kashia answered:

Using has_one

To add a Rooster to a user, use the following syntax:

user = User.create(name,pass,email)

user.rooster = Rooster.create(nrm)

This will add a new rooster object to the user and will set up the correct relations. Of course you could extend the user to use the method you described:

class User
  def create_rooster(nrm)
    rooster = Rooster.create(nrm)
  end
end

user = User.create(name,pass,email)
user.create_rooster(nrm)

has_many, many_to_many

This is analog to the has_one:

rooster = Rooster.new(nrm)
rooster.uren << Uur.create()
rooster.add_uren(Uur.create()) # alternate syntax

The second (and third) statement here will add the new Uur instance to the uren collection and will also set up the necessary connections.

Searching for relations

rooster = user.rooster
uren = rooster.uren

I hope I understood your question correctly here.

The first statement will return the correct Rooster object (SQL-Query: "SELECT * FROM ogrooster WHERE oid = #{@rooster_oid}")

The second one will do the same. It will return a Collection (which is almost like a normal Ruby Array) which you can then use for whatever you want.

Your second method actually would work, but your first statement looks much nicer, and thus works in Og. :P
A shortcut for your second method is Rooster[user.rooster_oid] instead of Rooster.find_by_oid(user.rooster_oid). But like I said, user.rooster will work just fine.

Update

Sorry that his didn't work. I'm already on 0.41 (which will be the next release).

On 0.31 I think someone else also tripped over that... I think it's a bug on 0.31 that belongs_to relations aren't set automatically.

Get around this by using:

user = User.create(name,pass,email)
r = Rooster.create(nmr)
user.rooster = r
r.user = user
r.save

class
  def create_rooster(nmr)
    save unless saved?
    rooster = Rooster.create(nrm)
    rooster.user = self
    rooster.save
  end
end

user.create_rooster(nmr)

It's a little longer but should do the job.

Rating: 5

basaah answered:

Thank you for your answer, but uhh I won't work. It does makes a new rooster object in the database but it leaves the relation ships field blank.

Maby I need a newer version of nitro/og. Edit[tried with the neewr 0.31(still had the 30)] and still the same problem

but I just did User.find_by_name('BAS').rooster = Rooster.create("aiaiai") (that user already existed) en the database created a new rooster ofcource but left useroid empty as well as the users roosteroid.

Is this a bug??

Edit: I found the og file for the has_one relation ship and found that there wasn't any real code in it.

Can anyone wo succesfully did this tell me what he did diffently or maby did exactly the same thing but what worked.

Damn I hate this, I want it to work.

UPDATE:

class 
  def create_rooster(nmr)
    save unless saved?
    rooster = Rooster.create(nmr)
    rooster.user = self
    rooster.save
    self.rooster =rooster
    self.save
  end
end

This actualy did it. I can now just do

user.create_rooster(nmr)
Now the user and the rooster autolinks to the correct oid. Perfect, thank you very much.

Rating: 0