私のモデルではOrganisation has_many :users、 と とUser has_and_belongs_to_many :rolesとRoleがnameありhas_and_belongs_to_many :usersます。
私のOrganisationクラスでは、を持っている人に属する をget_admin取得することになっているメソッドがあります。そのようです:UserOrganisationRole 'admin'
def get_admin
return self.users.with_role('admin')
end
ActiveRecord::Relation残念ながら、これはではなく オブジェクトを返しますUser。
.first私はそのように行末に追加しようとしました
def get_admin
return self.users.with_role('admin').first
end
しかし、私が得るのはSQLエラーだけです
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: role.name: SELECT "users".* FROM "users" WHERE ("users".organisation_id = 2) AND (role.name == 'admin') LIMIT 1
私のスキーマは次のように定義されています。
create_table "roles", :force => true do |t|
t.string "name", :null => false
end
create_table "users", :force => true do |t|
t.string "username", :null => false
t.integer "organisation_id"
end
create_table "roles_users", :id => false, :force => true do |t|
t.integer "user_id"
t.integer "role_id"
end
create_table "organisations", :force => true do |t|
t.string "name", :null => false
t.string "website", :null => false
end
Organisationのget_adminメソッドを (次のように) 実際に返すように書き直すにはどうすればよいUserですか?
def get_admin
return self.users.with_role('admin')
end
乾杯
デイブ