导轨2:联合多工作台

发布于 2025-01-05 23:34:46 字数 1888 浏览 0 评论 0原文

抱歉,我是新手,

我有数据库:

Migrate

-Mst_group tble

class CreateMstGroups < ActiveRecord::Migration
  def self.up
    create_table :mst_groups do |t|
      t.string :group_name
    end
  end
end

-Mst_japan

class CreateMstJapans < ActiveRecord::Migration
  def self.up
    create_table :mst_japans do |t|
      t.string :name_level
    end
  end
end

-Tbl_user

class CreateTblUsers < ActiveRecord::Migration
  def self.up
    create_table :tbl_users do |t|
      t.string :login_name, :null =>false,:limit =>15
      t.string :password,:null =>false,:limit =>50
      t.string :full_name,:null =>false
      t.string :full_name_kana
      t.string :email,:null =>false
      t.string :tel,:null =>false,:limit =>15
      t.date :birthday,:null =>false  

      t.references :mst_groups
    end
  end
end

-Tbl_detail_user_japan

class CreateTblDetailUserJapans < ActiveRecord::Migration
  def self.up
    create_table :tbl_detail_user_japans do |t|
      t.date :start_date
      t.date :end_date
      t.integer :total

      t.references :tbl_users
      t.references :mst_japans
    end
  end
end

模型

class MstGroup < ActiveRecord::Base
  has_many :tbl_users
end

class MstJapan < ActiveRecord::Base
  has_many :tbl_detail_user_japans
end

class TblUser < ActiveRecord::Base
  belongs_to :mst_group
  has_one :tbl_detail_user_japan
end

class TblDetailUserJapan < ActiveRecord::Base
  belongs_to :tbl_user
  belongs_to :mst_japan
end

控制器

def index
  @user= ???????
end

如何编写命令选择: login_name, full_name, full_name_kana, email, tel, group_name, name_lever, start_date, end_date, Total 在控制器中

Sorry, i'am newbie

I have database:

Migrate

-Mst_group tble

class CreateMstGroups < ActiveRecord::Migration
  def self.up
    create_table :mst_groups do |t|
      t.string :group_name
    end
  end
end

-Mst_japan

class CreateMstJapans < ActiveRecord::Migration
  def self.up
    create_table :mst_japans do |t|
      t.string :name_level
    end
  end
end

-Tbl_user

class CreateTblUsers < ActiveRecord::Migration
  def self.up
    create_table :tbl_users do |t|
      t.string :login_name, :null =>false,:limit =>15
      t.string :password,:null =>false,:limit =>50
      t.string :full_name,:null =>false
      t.string :full_name_kana
      t.string :email,:null =>false
      t.string :tel,:null =>false,:limit =>15
      t.date :birthday,:null =>false  

      t.references :mst_groups
    end
  end
end

-Tbl_detail_user_japan

class CreateTblDetailUserJapans < ActiveRecord::Migration
  def self.up
    create_table :tbl_detail_user_japans do |t|
      t.date :start_date
      t.date :end_date
      t.integer :total

      t.references :tbl_users
      t.references :mst_japans
    end
  end
end

Model

class MstGroup < ActiveRecord::Base
  has_many :tbl_users
end

class MstJapan < ActiveRecord::Base
  has_many :tbl_detail_user_japans
end

class TblUser < ActiveRecord::Base
  belongs_to :mst_group
  has_one :tbl_detail_user_japan
end

class TblDetailUserJapan < ActiveRecord::Base
  belongs_to :tbl_user
  belongs_to :mst_japan
end

Controller

def index
  @user= ???????
end

How to write command select : login_name, full_name, full_name_kana, email, tel, group_name, name_lever, start_date, end_date, total in controller

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

長街聽風 2025-01-12 23:34:46

这取决于您想要如何检索 User 对象。您需要告诉 Rails 如何找到 TblUser 对象。例如,如果用户 ID 已知,假设在一个名为“id”的变量中,那么您会这样做:

def index
  @user=TblUser.find(id)
end

这取决于您的应用程序逻辑,Rails 如何知道您需要哪个用户。在登录等情况下,您可能需要用户的输入。
(通常在 Rails 中,您会调用表“Users”,按照惯例表和类具有相同的名称,然后您不需要调用类 TblUser)

这就是控制器中需要的所有内容,您不需要告诉它你想要哪些字段。
然后在视图中您可以访问所有字段:
TblUser 上的字段,示例:

<%= @user.email %>

您可以通过关系访问相关对象的字段,示例:

<%= @user.mst_group.group_name %>

希望有助于您入门。

It depends on how you want to retrieve the User object. You need to tell Rails how to find the TblUser object. If, for example, the user ID is known, let's say in a variable called 'id' then you would do:

def index
  @user=TblUser.find(id)
end

It depends on your application logic how Rails would know which user you need. You may need an input from the user in case of log in, etc.
(Typically in Rails you would call the table 'Users', by convention tables and classes have the same name and then you wouldn't need to call the class TblUser)

That is all you need in the controller, you don't need to tell it which fields you want.
Then in the View you can access all the fields:
Fields on TblUser, example:

<%= @user.email %>

You can access the fields from related objects through the relations, example:

<%= @user.mst_group.group_name %>

Hope that helps to get you started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文