开发者

has_many returns an array instead of ActiveRecord class

I am following OmniAuth railscasts and trying to implement the same with authlogic + fa开发者_运维知识库cebook instead of devise + twitter as shown in the railscast.

Maybe my understanding of has_many still isn't good but in the railscasts ryan has the following code in AuthenticationsController

  def create
    auth = request.env["rack.auth"]
    current_user.authentications.find_or_create_by_provider_and_uid(auth['provider'], auth['uid'])
    flash[:notice] = "Authentication successful."
    redirect_to authentications_url
  end

In my implementation current_user.authentications returns an array [] how can I call find_or_create_by_provider_and_uid on an array?

Is my implementation wrong? Isn't has_many suppose to return an array?

Error I get is that I am calling find_or_create_by_provider_and_uid on a nil object.

current_user.authentications is nil well because the user does not have any authentications yet.


The array is actually an AssociationProxy instance, which delegates all method calls to the internal object, which is an array in the case of a has_many association (also see this question). This means you should be able to call magic methods like find_or_create_by_provider_and_uid, as well as scopes etc. on it just fine.

I found this question because I stumbled over a similar problem: for some reason I couldn't call ActiveRecord::Relation#klass to find out the model class:

post.comments.klass # => NoMethodError

But by calling relation first you can get a normal ActiveRecord::Relation instance:

post.comments.relation.klass # => Comment
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜