我想用新表更新我的用户模型,表格如下。 我需要用户在注册时选择一个位置。 我还需要用户能够在注册时选择最多3种他们喜欢听的音乐类型。
现在,问题是我要让用户选择他们喜欢哪种类型,我也希望他们能够点击类型链接,看看网站上的其他用户是否也喜欢这种类型。那么这意味着类型必须是一个模型,而位置应该是一个表?
这就是我在user.rb中的内容,
class User < ActiveRecord::Base
has_secure_password
before_save { self.email = email.downcase }
validates :first_name, :last_name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :first_name, :last_name, presence: true, uniqueness: true
validates_inclusion_of :age, in: 10..100
validates :password, presence: true, length: { minimum: 4 }, allow_nil: true
has_many :posts
has_attached_file :profile_picture, :styles => { :medium => "300x300>", :thumb => "100x100>" },
:default_url => "app/assets/images/missing.png",
:path => ":rails_root/public/system/:class/:attachment/:id_partition/:style/:filename"
validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/.*\Z/
def self.search(query)
where("email like ?", "%#{query}%")
end
end
如果你们需要任何其他代码,请告诉我,并提前感谢!
答案 0 :(得分:0)
如果您要求帮助,您至少可以做的是清理间距和缩进。将所有验证分组在一起等。
至于您希望如何设置登录过程,您似乎正在构建自己的登录过程。您可以随意发布与登录过程相关的所有文件,但使用设计和定制视图(click for tutorial)会更容易。您可以让您的用户添加他们的位置&amp;这里的流派偏好。
在我再继续之前,让我们进入相同的页面进行定义。我认为你的意思是上述问题中的列而不是表格,但如果我误解了你,请告诉我。
继续前进:您可以通过多种方式设置自己的流派。我建议您创建一个新的类型表(不是列),并通过&#39;到&#39;与您的用户关联。 table(click for solution)我知道这个链接很旧,但它会让你朝着正确的方向前进。这可能对你来说很重要,特别是如果你之前没有做过关联但是试一试。
如果所有其他方法都失败了,您可以减少要开始使用的功能数量,并询问用户是否需要该功能。祝你好运!
答案 1 :(得分:0)
您最好使用流派制作新的流派模型。当用户想要选择其位置时,您可以在用户只选择一个位置时添加has_one关联。
<强> genre.rb 强>
属性:id,name,decription
class Genre < ActiveRecord::Base
has_many: genres_users
has_many: users, through: genres_users
end
<强> genres_user.rb 强>
属性:id,user_id,genre_id
class GenresUser < ActiveRecord::Base
belongs_to :user
belongs_to :genre
end
<强> user.rb 强>
属性:您的属性字段
class User < ActiveRecord::Base
has_many: genres_users
has_many: genres, through: genres_users
has_one: location
end
<强> location.rb 强>
class Location < ActiveRecord::Base
belongs_to :user
end
的详细信息
答案 2 :(得分:0)
我想用新表格更新我的用户模型
正如其他答案所述,每个Model 应该代表一个表。
情况并非总是如此; STI models不会有表,您可以像使用类一样使用模型(没有数据库集成)。
但是,重点是您不能只是将表“添加”到模型中。您可以添加关联,这是一个完全不同的概念。我会在这里解释一下:
<强> ORM 强>
为了更好地理解您的要求,有助于了解Rails是abstraction layer的relational database&amp;网络服务器软件。这意味着当您处理数据时,您不是从数据库中提取,而是处理可以以各种方式填充的对象。
Rails是一个MVC( M odel V iew C ontroller)框架:
这意味着您的“数据”不依赖于表等,它与模型绑定,这些模型旨在为您提供可在应用程序的业务逻辑中使用的对象
换句话说,您不应该将Rails应用程序视为访问数据的方式,而是要操纵对象。
这是通过名为ORM的ActiveRecord来完成的。当您调用User.find
之类的内容时,ActiveRecord负责,并且位于Rails'ActiveRecord associations之后。
您真正要求的是“如何将关联添加到我的User
模型中?”
-
我需要用户在注册时选择一个位置。
我还要求用户在注册时能够选择最多3种他们喜欢听的音乐类型。
这就是我要做的事情:
#app/models/user.rb
class User < ActiveRecord::Base
# columns id | user | etc | location_id | created_at | updated_at
belongs_to :location
has_and_belongs_to_many :genres
end
#app/models/location.rb
class Location < ActiveRecord::Base
# columns id | etc | etc | created_at | updated_at
has_many :users
end
#app/models/genre.rb
class Genre < ActiveRecord::Base
has_and_belongs_to_many :users
end
现在,还有另一个答案提到了一个名为genre_user.rb
的模型。这是针对has_many :through
的,我认为不适用于这种情况。
我建议使用has_and_belongs_to_many
联接表,只需验证genres
到3的数量(我会让你解决这个问题):
为此,您需要一个名为genres_users
的表,其中包含genre_id
和user_id
属性。这将使您能够致电:
$ @user = User.find 1
$ @user.genres #-> 1,2,6