Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 修改关系关联_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 3 修改关系关联

Ruby on rails 3 修改关系关联,ruby-on-rails-3,Ruby On Rails 3,我在一个微型社交网络上工作,我有两种不同的账户类型,用户可以是个人资料或页面 class User < ActiveRecord::Base has_one :profile has_one :page end 但它不起作用 非常感谢您的帮助。非常感谢 我不太确定你在问什么,但是你可以在用户模型中添加一个方法来处理这个问题: class User < ActiveRecord::Base has_one :profile has_one :page de

我在一个微型社交网络上工作,我有两种不同的账户类型,用户可以是个人资料或页面

class User < ActiveRecord::Base

  has_one :profile
  has_one :page

end
但它不起作用


非常感谢您的帮助。非常感谢

我不太确定你在问什么,但是你可以在
用户
模型中添加一个方法来处理这个问题:

class User < ActiveRecord::Base

  has_one :profile
  has_one :page

  def name
     if self.account_type == 'profile'
        return self.profile.name

     return <whatever for page>
  end
end
要使用它,假设我们有
a=User.find(:id)
和任何
id
。然后,假设
a
account\u type
profile
,您就可以这样做了:


a.get_info(:name)#=>a.profile.name

是的,我有很多字段,比如profile有姓名、年龄、性别等等。然后,页面上有名字,描述,地址,国家等等。如果你想这样做的话,我会添加更多。是的,但现在我做了current_user.name,什么也没有得到,或者我用错了吗?这是我第一次看到method_missing,我删除了method_缺少的内容,因为最好创建一个自定义方法来处理这个问题。太棒了。我真的很感激你的帮助!这有点让人困惑——如果帐户类型是配置文件,为什么要将其配置文件设置为页面?另外,我不确定
@current\u user.profile=@current\u user.page
会做什么,但是
@current\u user.profile
会将外键存储到
profile
,因此如果您真的想这样做,您需要存储
page
id
。我正在尝试删除当前\u用户的“profile”变量,他没有,并将配置文件设置为页面。因此,我不必在每个视图中的current_user.profile和current_user.page之间进行更改。相反,我只使用当前的_user.profile来处理这两个问题
class User < ActiveRecord::Base

  has_one :profile
  has_one :page

  def name
     if self.account_type == 'profile'
        return self.profile.name

     return <whatever for page>
  end
end
class User < ActiveRecord::Base

  # other code removed

  def get_info(method, *args)
      if self.account_type == 'profile'
          return self.profile.send(method, *args)
      end
      self.page.send(method, *args)
  end 
end