为rails指定表.where

时间:2017-07-01 15:53:21

标签: ruby-on-rails ruby

无法找到相同的问题。如何为模型指定select.where?

我需要从一个模型中选择不同的表,并希望在控制器中得到这样的东西:

ngInputString

我怎样才能得到这样的东西?

感谢您的回答!

UPD:

我有一个用户表和许多带有消息的表(每个表用于一对用户)。在用户表中,有“消息”和“消息”。带有消息表id的列。在user_controller中我需要像我的问题一样运行查询。也许有人可以分享一个例子吗?

1 个答案:

答案 0 :(得分:1)

如何改变设计,只需2个表(用户和消息)就可以了解下面的详细信息 用户表(id,name) 消息表(user_id,message_text) 您设置了关系用户has_many消息(请参阅此链接以获取更多指南http://guides.rubyonrails.org/association_basics.html#the-has-many-association

user.rb

has_many  :memberships

message.rb

belongs_to :user
例如,您需要访问具有特定ID的用户以及该用户的消息 在users_controller.rb中

  def show
    @user     = User.find(params[:id])
    # this find user
    @messages = @user.messages
    # get all the messages for specific users
  end