3

私は Ruby on Rails を初めて使用し、特定の組織からスコアにアクセスしようとしています。誰かがこれを達成できる正しい方向に私を向けることができるかどうか疑問に思っていました. 現在、次のモデルがあります。

ユーザーモデルはこちら

class User < ActiveRecord::Base
 attr_accessible :fname, :lname, :email, :password, :password_confirmation
 has_secure_password
 belongs_to :organization

組織モデルはこちら

class Organization < ActiveRecord::Base
 attr_accessible :name, :employee_number, :country, :postal_code, :sic_code, :primary_url
 has_many :users
 has_many :social_entities
 has_many :social_scores, :through => :social_entity  
 has_many :social_channels, :through => :social_entity

これがエンティティモデルです

class SocialEntity < ActiveRecord::Base
 attr_accessible :name, :org_id
 has_many :social_channels
 has_many :social_scores  
 belongs_to :organization

チャンネルモデルはこちら

class SocialChannel < ActiveRecord::Base
 attr_accessible :authorized, :channel_identifier, :channel_type, :name, :social_entity_id
 belongs_to :social_entity  # edited from original post, :socialentity

スコアモデルはこちら

class SocialScore < ActiveRecord::Base
 attr_accessible :engagement_score, :popularity_score, :presence_score, :reputation_score,   :score_period_end, :score_period_start, :score_period_type, :score_timestamp, :social_entity
 belongs_to :social_entity

これが私がやろうとしていることの説明です。ユーザーはシステムにログインし、ユーザーは組織に関連付けられ、各組織にはソーシャル チャネルとスコアを持つソーシャル エンティティがあります。組織のスコアをビューに表示できるようにしたいと考えています。

4

1 に答える 1

3

Organization のインスタンスがある場合、宣言は、has_manyによって参照される SocialScores のコレクションを取得する属性 (実際にはメソッド) を作成します。social_scores

@org = Organization.find(:first)  # or however else it is that you get the organization instance
@org.social_scores.each do |ss|
   puts "Popularity score for id #{ss.id} is #{ss.popularity_score}"
end

それはあなたが求めていたものですか?

于 2012-11-30T19:49:13.027 に答える