简体   繁体   English

具有has_one关联的嵌套模型

[英]Nested Model with a has_one association

I have a few models...User model, A collection model and an inspiration model... 我有几个型号......用户模型,收集模型和灵感模型......

The collection model has_one inspiration 收藏模型具有灵感

has_one :inspiration, :dependent => :destroy
belongs_to :user

the inspiration model belongs to a collection 灵感模型属于一个集合

belongs_to :collection
belongs_to :user

here are my routes 这是我的路线

resources :collections do
  resources :inspiration
end

Here is my InspirationController (it is not plural) 这是我的InspirationController(它不是复数)

def index
  @collection = Collection.find(params[:collection_id])
  @inspiration = @collection.inspiration
end

def new
  if signed_in? && current_user == @collection.user
    @user = current_user
    @collection = @user.collections.find(params[:collection_id])
    @inspiration = @collection.inspiration.new
  elsif signed_in? && current_user != @collection.user
    flash[:error] = "That's not your collection."
    redirect_to root_url
  else
    flash[:error] = "Please sign in."
    redirect_to login_url
  end
end

My view for inspiration is also singular (not inspirations) 我对灵感的看法也是单一的(不是灵感)

In all the time I've been using Rails which isnt too terribly long I haven't had to use the has_one association and now I'm coming up with some errors... 在我一直使用Rails的过程中,我不必使用has_one关联,现在我想出了一些错误......

When i view the pages I either get one of two errors... 当我查看页面时,我得到两个错误之一...

One is an undefined method of inspiration which comes from the second line of each action in the InspirationController...the other says an undefined method COUNT, because I have an if statement in the view 一个是未定义的灵感方法,来自InspirationController中每个动作的第二行...另一个是未定义的方法COUNT,因为我在视图中有一个if语句

if @collection.inspiration.count > 0 
   foobar foobar
end 

In the rails console, when i try to find the count of the inspirations of a specific collection, i can see that it isn't even performing the proper query.... 在rails控制台中,当我试图找到特定集合的灵感计数时,我可以看到它甚至没有执行正确的查询....

can anyone shed some light on this problem or point me to a good resource to read up on this type of association...thank you in advance 任何人都可以对这个问题有所了解,或者指出一个很好的资源来阅读这种类型的关联...提前谢谢你

one thing to point out which i did different than I normally do for has_many associations... 1. i used the singular "inspiration" in place of the plural "inspirations" in quite a few places including the routes, view, and controller. 有一点需要指出我的做法与我通常为has_many协会所做的不同...... 1.我在很多地方使用了单数的“灵感”代替了复数的“灵感”,包括路线,视图和控制器。

Here is an EDIT when trying to create a new inspiration, I get the following error 这是一个编辑,当试图创造一个新的灵感时,我得到以下错误

undefined method `inspiration'

which points to the new action 这指向新的行动

@user = current_user
@collection = @user.collections.find(params[:collection_id])
@inspiration = @collection.**inspiration**.new

the word inspiration in between the stars in where the problem is occuring 在出现问题的星星之间的灵感这个词

cheers, Justin 欢呼,贾斯汀

Since collection has_one inspiration, @collection.inspiration return an inspiration object, not a collection of inspiration (did you have to call your model collection, now I'm mumbling :P). 由于收集has_one灵感, @collection.inspiration返回一个灵感对象,而不是灵感的集合(你必须打电话给你的模型集合,现在我是在喃喃自语:P)。 Instead do: 相反:

if @collection.inspiration
  foobar foobar
end

also you can't do @inspiration = @collection.inspiration.new as inspiration is nil. 你也做不到@inspiration = @collection.inspiration.new因为灵感是零。 Instead do: 相反:

@inspiration = @collection.build_inspiration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM