2010-12-30 9 views
0
class CreateActivities < ActiveRecord::Migration 
    def self.up 
    create_table :activities do |t| 
     t.references :user 
     t.references :media 
     t.integer :artist_id 

     t.string :type 
     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :activities 
    end 
end 

class Fan < Activity 
    belongs_to :user, :counter_cache => true 
end 

class Activity < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :media 
    belongs_to :artist, :class_name => 'User', :foreign_key => 'artist_id' 
end 

class User < ActiveRecord::Base 
    has_many :activities 
    has_many :fans 
end 

Ich habe versucht, zu meinem Aktivitätsmodell zu ändern, ohne Erfolg:Hilfe mit aktiven Datensatz Beziehungen

class Activity < ActiveRecord::Base 
    has_many :activities, :class_name => 'User', :foreign_key => 'user_id' 
    has_many :activities, :class_name => 'User', :foreign_key => 'artist_id' 
end 

Eine Sache zu beachten. Aktivität ist ein STI. Fan erbt von Aktivität.

In Konsole, ich mache:

# Create a fan object. User is a fan of himself 
fan = Fan.new 
=> #<Fan id: nil, user_id: nil, media_id: nil, artist_id: nil, type: "Fan", comment: nil, created_at: nil, updated_at: nil> 

# Assign a user object 
fan.user = User.first 
=> #<User id: 1, genre_id: 1, country_id: 1, .... 

# Assign an artist object 
fan.artist_id = User.first.id 
=> 1 

# Save the fan object 
fan.save! 
=> true 

Activity.last 
=> #<Fan id: 13, user_id: 1, media_id: nil, artist_id: 1, type: "Fan", comment: nil, created_at: "2010-12-30 08:41:25", updated_at: "2010-12-30 08:41:25"> 

Activity.last.user 
=> #<User id: 1, genre_id: 1, country_id: 1, ..... 

Aber ...

Activity.last.artist 
=> nil 

Warum ist Activity.last.artist Rückkehr null?

+0

welche SQL-Anfrage wird von Ihrer fehlgeschlagenen Befehlszeile generiert? – shingara

+0

Können Sie die SQL-Abfragen aus development.log einfügen, die von diesen Abfragen generiert wurden? –

+0

Entwicklung.log ist leer. Ich denke, dass Konsolenbefehle nicht in der Entwicklung enthalten sind. Log –

Antwort

0

Ok. Es scheint, ich hatte

attr_accessor :artist 

in meinem Fan-Modell. Entfernt es und funktioniert nun entsprechend

+0

cool - Sie sollten das wahrscheinlich dann schließen, um Menschen zu retten, die durch den ganzen Code lesen, nur um herauszufinden, dass Sie es gelöst haben! – stef

0

Was ich feststellen, ist der KUENSTLER_ID ist immer Null, wenn ich versuche, das Fan-Objekt .Create zu erstellen !, als solche:

Fan.create!(:user => User.first, :artist => User.first.id) 
=> #<Fan id: 43, user_id: 4, media_id: nil, artist_id: nil, type: "Fan", comment: nil, created_at: "2010-12-30 10:05:19", updated_at: "2010-12-30 10:05:19"> 

Fan.create!(:user => User.first, :artist_id => User.first.id) 
=> #<Fan id: 44, user_id: 4, media_id: nil, artist_id: nil, type: "Fan", comment: nil, created_at: "2010-12-30 10:06:33", updated_at: "2010-12-30 10:06:33"> 

Fan.create!(:user => User.first, :artist => User.first) 
=> #<Fan id: 45, user_id: 4, media_id: nil, artist_id: nil, type: "Fan", comment: nil, created_at: "2010-12-30 10:06:59", updated_at: "2010-12-30 10:06:59"> 

Warum ist das?

Verwandte Themen