Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.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 理解rails 3中的关联_Ruby On Rails_Ruby_Ruby On Rails 3_Associations_Models - Fatal编程技术网

Ruby on rails 理解rails 3中的关联

Ruby on rails 理解rails 3中的关联,ruby-on-rails,ruby,ruby-on-rails-3,associations,models,Ruby On Rails,Ruby,Ruby On Rails 3,Associations,Models,似乎我需要重温一下我在rails中的关联。目前,我正在尝试显示所有以部门名称作为职员的职位 目前存在两种模式,即员额和部门 class Post < ActiveRecord::Base belongs_to :department attr_accessible :title, :comments, :department_id end class Department < ActiveRecord::Base has_many :posts attr_acces

似乎我需要重温一下我在rails中的关联。目前,我正在尝试显示所有以部门名称作为职员的职位

目前存在两种模式,即员额和部门

class Post < ActiveRecord::Base
  belongs_to :department
  attr_accessible :title, :comments, :department_id
end

class Department < ActiveRecord::Base
  has_many :posts
  attr_accessible :name, :post_id
  #Scopes
  scope :staff_posts, where(:name => "Staff") 
end

在控制器中,它返回名为staff的部门。您在department对象上使用了标题和注释,这就是为什么它给出了nil方法错误

这样使用:

 def staffnews
   @dept_staff = Department.staff_posts
 end

 <% @dept_staff.each do |ds| %>
   <% ds.posts.each do |p| %>
     <h2><%= p.title %></h2>
     <h2><%= p.comments %></h2>
   <% end %>
 <% end %>
控制器:

def staffnews
  @staffpost = Post.staff_posts
end
视图:#无变化

<% @staffpost.each do |t| %>
  <h2><%= t.title %></h2>
  <h2><%= t.comments %></h2>
<% end %>


在控制器中,它返回名为staff的部门。您在department对象上使用了标题和注释,这就是为什么它给出了nil方法错误

这样使用:

 def staffnews
   @dept_staff = Department.staff_posts
 end

 <% @dept_staff.each do |ds| %>
   <% ds.posts.each do |p| %>
     <h2><%= p.title %></h2>
     <h2><%= p.comments %></h2>
   <% end %>
 <% end %>
控制器:

def staffnews
  @staffpost = Post.staff_posts
end
视图:#无变化

<% @staffpost.each do |t| %>
  <h2><%= t.title %></h2>
  <h2><%= t.comments %></h2>
<% end %>

您的
员工岗位
范围仅选择名为“员工”的部门。假设您将有一个且只有一个名为staff的部门,您有几种方法来处理此问题

这将找到所有名为staff的部门,并加载相应的职位:

@department = Department.where(name: "Staff").include(:posts).first
然而,由于您正试图确定Post的范围,所以这属于Post。下面是一个使用方法作为作用域的示例:

class Post < ActiveRecord::Base
  belongs_to :department
  attr_accessible :title, :comments, :department_id

  def self.staff
    where(department_id: staff_department_id)
  end

  def staff_department_id
    Department.find_by_name!("Staff").id
  end

end
class Post

通过这种方式,您可以使用
@staff\u posts=Post.staff
并迭代该集合(注意:我不建议以这种方式永久获取
staff\u department\u id
。这可以在应用程序启动时设置为常量,或者其他更健壮的解决方案).

您的
员工岗位
范围仅选择名为“员工”的部门。假设您将有一个且只有一个名为staff的部门,您有几种方法来处理此问题

这将找到所有名为staff的部门,并加载相应的职位:

@department = Department.where(name: "Staff").include(:posts).first
然而,由于您正试图确定Post的范围,所以这属于Post。下面是一个使用方法作为作用域的示例:

class Post < ActiveRecord::Base
  belongs_to :department
  attr_accessible :title, :comments, :department_id

  def self.staff
    where(department_id: staff_department_id)
  end

  def staff_department_id
    Department.find_by_name!("Staff").id
  end

end
class Post

通过这种方式,您可以使用
@staff\u posts=Post.staff
并迭代该集合(注意:我不建议以这种方式永久获取
staff\u department\u id
。这可以在应用程序启动时设置为常量,或者其他更健壮的解决方案).

您可以通过以下更改找到所有部门名称为staff的职位:

类publicpagescocontroller
def staffnews
    #get all the department which have name is staff
    departments = Department.where("name=?","staff")

    #get all the ids
    department_ids = departments.map(&:id)

    #retrieve post that department name is staff 
    @staffpost = Post.find_by_department_id(department_ids)
end

结束

您可以通过以下更改找到所有具有部门名称staff的职位:

类publicpagescocontroller
def staffnews
    #get all the department which have name is staff
    departments = Department.where("name=?","staff")

    #get all the ids
    department_ids = departments.map(&:id)

    #retrieve post that department name is staff 
    @staffpost = Post.find_by_department_id(department_ids)
end

结束

“看来我需要重新审视一下我的联想”——为此我推荐一本书。结构化知识比帖子更有用。在controller中,它返回的部门名为staff。并且您在department对象上使用标题和注释,这就是没有方法错误的原因coming@SergioTulentsev除了常规的rails文档之外,还有什么建议吗(你是否遇到过简化解释的博客或帖子?)我不确定,而且我现在无法测试,因为我不在笔记本电脑旁。但据我所知,您已经在部门模型中声明了范围,
Department.staff\u posts
将返回名为“staff”的部门。我不确定,但是
@staffposts=Department.staff\u posts.first.posts
应该获取这些帖子。如果这不起作用,那么你可以使用
@staffposts=Department.find_by_name('staff')。posts
“似乎我需要复习一下我的联系”——我推荐一本这方面的书。结构化知识比帖子更有用。在controller中,它返回的部门名为staff。并且您在department对象上使用标题和注释,这就是没有方法错误的原因coming@SergioTulentsev除了常规的rails文档之外,还有什么建议吗(你是否遇到过简化解释的博客或帖子?)我不确定,而且我现在无法测试,因为我不在笔记本电脑旁。但据我所知,您已经在部门模型中声明了范围,
Department.staff\u posts
将返回名为“staff”的部门。我不确定,但是
@staffposts=Department.staff\u posts.first.posts
应该获取这些帖子。如果这不起作用,那么您可以使用
@staffposts=Department.find_by_name('staff')。posts
。根据变量的基数命名变量被认为是一种良好的做法。您的第一行最好写为
departments=Department.where(“name=?”,“staff”)
,并相应地更改下一行。只是一个无关的评论。谢谢,正如你提到的,我已经更改了变量的名称。根据其基数命名变量被认为是一种良好的做法。您的第一行最好写为
departments=Department.where(“name=?”,“staff”)
,并相应地更改下一行。只是一个无关的评论。谢谢,我已经更改了变量的名称,正如你提到的。