为什么Ruby on Rails不能找到这个类?

时间:2021-01-08 20:41:00

Okay. So I have three models, a, b and c. a has_one c, b has_many cs, and c belongs_to both a and b. When I reference "cs" in a method of b, it comes out fine. But when I reference "c" in a method of a, it can't find the reference; it says "c" is an 'undefined local variable or method'. I know that c objects are getting created, because they appear in the MySQL database, and I can access them perfectly well from the b model, but I can't access them from the a model.

好的。所以我有三个模型,a,b和c。一个has_one c,b has_many cs,c属于a和b。当我用b的方法引用“cs”时,它很好。但是当我在a的方法中引用“c”时,它找不到引用;它说“c”是一个'未定义的局部变量或方法'。我知道c对象正在创建,因为它们出现在MySQL数据库中,我可以很好地从b模型访问它们,但我无法从模型中访问它们。

2 个解决方案

#1


Your problem comes from your specification of the relationships.

您的问题来自您对关系的规范。

It seems you're looking for something along these lines

看来你正在寻找这些方面的东西

class a < ActiveRecord::Base
  has_one :c, :through=>:b
end


class b < ActiveRecord::Base
  has_many :c
end


class a < ActiveRecord::Base
  belongs_to :b
end

That should allow you to run the query properly. Hope this helps.

这应该允许您正确运行查询。希望这可以帮助。

#2


class A < ActiveRecord::Base has_one :c, :dependent => :destroy

class A :destroy

class B < ActiveRecord::Base has_many :c

B类

class C < ActiveRecord::Base belongs_to :a belongs_to :b

class C

Create method in as_controller:

在as_controller中创建方法:

def create @a = a.new(params[:a]) params[:b][:venue_id] = get_venue_id_for_b unless params[:venue][:name].blank? @a.user = @user @a.b = B.new(params[:b]) @b = @a.b @b.valid? @a.valid?

def创建@a = a.new(params [:a])params [:b] [:venue_id] = get_venue_id_for_b除非params [:venue] [:name] .blank? @ a.user = @user @ a.b = B.new(params [:b])@b = @ a.b @ b.valid? @一个有效的?

respond_to do |format|
  if @a.valid? and @b.valid?
    if (@a.duplicate? or @a.save) and @b.save
      flash[:notice] = 'a was successfully created.'
      format.html { redirect_to a_url(@a) }
      format.xml  { head :created, :location => a_url(@a) }
    else
      format.html { render :action => "new" }
      format.xml { render :xml => @a.errors.to_xml and @b.errors.to_xml }
    end
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @a.errors.to_xml and @b.errors.to_xml }
  end
end

end

This method within model B works:

模型B中的此方法有效:

def q puts cs.class end

def q将cs.class结束

This method within model A does not work:

模型A中的此方法不起作用:

def q puts c.class end

def q把c.class结束

#1


Your problem comes from your specification of the relationships.

您的问题来自您对关系的规范。

It seems you're looking for something along these lines

看来你正在寻找这些方面的东西

class a < ActiveRecord::Base
  has_one :c, :through=>:b
end


class b < ActiveRecord::Base
  has_many :c
end


class a < ActiveRecord::Base
  belongs_to :b
end

That should allow you to run the query properly. Hope this helps.

这应该允许您正确运行查询。希望这可以帮助。

#2


class A < ActiveRecord::Base has_one :c, :dependent => :destroy

class A :destroy

class B < ActiveRecord::Base has_many :c

B类

class C < ActiveRecord::Base belongs_to :a belongs_to :b

class C

Create method in as_controller:

在as_controller中创建方法:

def create @a = a.new(params[:a]) params[:b][:venue_id] = get_venue_id_for_b unless params[:venue][:name].blank? @a.user = @user @a.b = B.new(params[:b]) @b = @a.b @b.valid? @a.valid?

def创建@a = a.new(params [:a])params [:b] [:venue_id] = get_venue_id_for_b除非params [:venue] [:name] .blank? @ a.user = @user @ a.b = B.new(params [:b])@b = @ a.b @ b.valid? @一个有效的?

respond_to do |format|
  if @a.valid? and @b.valid?
    if (@a.duplicate? or @a.save) and @b.save
      flash[:notice] = 'a was successfully created.'
      format.html { redirect_to a_url(@a) }
      format.xml  { head :created, :location => a_url(@a) }
    else
      format.html { render :action => "new" }
      format.xml { render :xml => @a.errors.to_xml and @b.errors.to_xml }
    end
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @a.errors.to_xml and @b.errors.to_xml }
  end
end

end

This method within model B works:

模型B中的此方法有效:

def q puts cs.class end

def q将cs.class结束

This method within model A does not work:

模型A中的此方法不起作用:

def q puts c.class end

def q把c.class结束