Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.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 没有方法错误_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 4_Has And Belongs To Many - Fatal编程技术网

Ruby on rails 没有方法错误

Ruby on rails 没有方法错误,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,has-and-belongs-to-many,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,Has And Belongs To Many,我正在rails 4中的一个项目中工作,在这个项目中,一个用户可以有多个项目,并且许多人与单个项目关联。项目和用户共享多对多关系 我使用此迁移代码创建了一个新表- class CreateProjectsUsersJoin < ActiveRecord::Migration def change create_table :projects_users_joins, :id => false do |t| t.integer :user_id

我正在rails 4中的一个项目中工作,在这个项目中,一个用户可以有多个项目,并且许多人与单个项目关联。项目和用户共享多对多关系

我使用此迁移代码创建了一个新表-

class CreateProjectsUsersJoin < ActiveRecord::Migration
  def change
    create_table :projects_users_joins, :id => false do |t|

        t.integer :user_id
        t.integer :project_id
    end

    add_index :projects_users_joins, ["user_id","project_id"]
  end
end
class CreateProjectsUserJoinfalse do | t|
t、 整数:用户id
t、 整数:项目id
结束
添加索引:项目用户加入,[“用户id”,“项目id”]
结束
结束
并以以下方式加入协会—

class Project < ActiveRecord::Base

    validates :name , :presence => true
    validates :description, :presence => true


    #associations
    has_and_belongs_to_many :users

end


class User < ActiveRecord::Base

    include CarrierWave::MiniMagick

    #removes the white spaces before validating the data 


    before_validation :strip_whitespace, :only => [:name,:email]

    #data validations
    validates :email, :presence =>true,  :uniqueness => {case_sensitive: false}, :format => { :with=> /([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/, :message => "please enter a valid e-mail" }
    validates :name, :presence=>true
    validates :password ,:presence =>true, :confirmation=> true #, :length =>{ :minimum=>6, :maximum=>30}, :format=>{:with=>/(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,30}/}

    #for the image
    mount_uploader :image, ImageUploader

    #for the password
    has_secure_password


    #associations
    has_many :issues
    has_many :comments
    has_and_belongs_to_many :projects

end


    #squishes the data(removes heading and trailing  white spaces and replaces multiple space by songle)
def strip_whitespace
    self.email = self.email.squish
    self.name = self.name.squish
end
class项目true
验证:description,:presence=>true
#联想
_和_属于_许多:用户吗
结束
类用户[:name,:email]
#数据验证
验证:email,:presence=>true,:university=>{区分大小写:false},:format=>{:with=>/([a-zA-Z0-9\-\.]+@(\[[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[1,3}.)))([a-zA Z0-Z0-9\]++.+)([a-zA Z-Z]{2,4}.[0-9]{1,3}.]请输入有效的电子邮件([]
验证:name,:presence=>true
验证:password、:presence=>true、:confirmation=>true#、:length=>{:minimum=>6、:maximum=>30}、:format=>{:with=>/(?=.*\d)(?=.[a-z])(?=.[a-z])(?=.[a-z])。{6,30}/}
#为了形象
挂载上传器:图像,图像上传器
#密码
有安全的密码吗
#联想
有很多问题
有很多评论
_和_属于_许多项目吗
结束
#压缩数据(删除标题和尾随空格,并用宋体替换多个空格)
def strip_空格
self.email=self.email.squish
self.name=self.name.squish
结束
当我在rails控制台上运行以下命令时,我得到一个无方法错误- 请帮忙

pro = Project.find(1)  (works)
me = User.find(27) (works)
pro.users << me  (throws a no method defined error)
pro=Project.find(1)(工程)
me=User.find(27)(工作)

pro.users您使用了错误的表名。默认的联接表名称应该只是两个类的复数名称,按词法顺序用下划线联接。因此,在这种情况下,
projects\u用户

如果您想要
项目\u用户\u连接
,那么您可以在
habtm
调用中提供
:join\u table
选项,或者(我的建议)执行该迁移的
rake db:migrate:down
,然后删除
“\u连接”
,然后再次迁移以获得默认表名