`
clark1231
  • 浏览: 253525 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

has_and_belongs_to_many

阅读更多

  users              groups        groups_users

id id group_id
name name user_id
description description

 

表groups_users只存在表结构,没有相应的model,controller,views等

表groups_users的生成(该表没有id)

 

  def change
    create_table(:groups_users, :id => false) do |t|
      t.integer :group_id
      t.integer :user_id

      t.timestamps
    end
  end

model中

 

class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
  attr_accessor :user_id
end


class User < ActiveRecord::Base
  has_and_belongs_to_many :groups
end
 

 groups的show(haml)

 

= semantic_attributes_for @group do |attr|
  = attr.attributes do
    = attr.attribute :name
    = attr.attribute :description

  = attr.attributes "group_users" do
    - if @group.users
      - @group.users.each do |u|
        = attr.attribute "user:", :value => u.name
        = button_to "删除组员", { :action => "delete_groupmember", :id => @group.id, :user_id => u.id,
          :confirm => "您确定要删除吗?", :method => :delete}

= semantic_form_for(@group, :url => {:action => "add_user_to_group"}, :html => {:method => :post}) do |f|
  = f.inputs do
    = f.hidden_field :id
    = f.input :user_id, :collection => User.all
  = f.buttons do
    = f.commit_button

 groups中的controller

 

  def add_user_to_group
    @group = Group.find(params[:group][:id])
    
    unless params[:group][:user_id].blank?
      @user = User.find(params[:group][:user_id])
      @group.users << @user unless @group.users.any?{ |u| u == @user }
      redirect_to @group, :notice => I18n.t("flash.groups.grouping_success.notice")
    else
      redirect_to @group, :notice => I18n.t("flash.groups.grouping_failed.notice")
    end
  end

 方法push_with_attributes( )<<方法的作用一样,都是给两个Model之间设置连接

 

  def delete_groupmember
    @group = Group.find(params[:id])
    @user = User.find(params[:user_id])
    @group.users.delete(@user)
    redirect_to @group, :notice => I18n.t("flash.groups.delete_groupmember.notice")
  end
 

 

或者通过checkboxes来实现HABTM关系

 

<% for group in Group.find(:all) %>  
    <div>  
      <%= check_box_tag "user[group_ids][]", category.id, @uer.groups.include?(group) %>  
      <%= group.name %>  
    </div>  
<% end %>  

controller中

 

params[:user][:group_ids] ||= [] 

 具体请见:

http://asciicasts.com/episodes/17-habtm-checkboxes

http://asciicasts.com/episodes/258-token-fields

0
0
分享到:
评论

相关推荐

    activemodel-associations, 用于普通 ruby 对象的has_many和belongs_to宏.zip

    activemodel-associations, 用于普通 ruby 对象的has_many和belongs_to宏 ActiveModel::Associations 用于普通 ruby 对象的has_many 和 belongs_to 宏。安装将此行添加到你的应用程序的Gemfile中:gem 'activemodel-

    关于Rails中的表关联的程序

    Rails提供了四种基本的关联类型:` belongs_to`、` has_one`、` has_many` 和 `has_and_belongs_to_many`。这些关联允许我们建立对象之间的关系,从而在编程时简化数据的存取。 1. `belongs_to` 关联: 这种关联...

    Ruby on Rails中的ActiveRecord编程指南

     偏好 has_many :through 胜于 has_and_belongs_to_many。 使用 has_many :through 允许在 join 模型有附加的属性及验证   # 使用 has_and_belongs_to_many class User &lt; ActiveRecord::Base has_and_...

    fixture_dependencies:SequelActiveRecord夹具加载器,用于处理依赖关系图

    支持many_to_one / belongs_to,one_to_many / has_many,many_to_many / has_and_belongs_to_many和has_one / one_to_one关联 以不违反外键约束的方式加载灯具的依赖关系图 有一个非常简单的API...

    Pro Active Record. Databases with Ruby and Rails

    4. **关联**:深入研究Active Record的各种关联类型,如has_many、belongs_to、has_one、has_and_belongs_to_many,以及如何处理关联查询和嵌套关联。 5. **事务**:学习如何使用Active Record事务来保证数据库操作...

    SinatraStreetFighter:欢迎使用我简化的Street Fighter模拟版本。 您可以选择一个角色,添加战斗动作以及史诗般的战斗开始的最后阶段!

    特征 楷模用户,角色,移动,舞台用户has_many:字符字符belongs_to :用户字符has_many :动作字符has_many :stages, through: ::moves 阶段has_and_belongs_to_many :移动阶段has_many :characters, ...

    ActiveRecord简单实例代码.zip

    ActiveRecord支持多种关联关系,如`has_many`、`belongs_to`、`has_one`和`has_and_belongs_to_many`。这些关联定义了不同模型之间的关系,例如,一个`User`模型可能`has_many``Posts`,表示用户可以有多个帖子。 ...

    ruby on rails在线考试系统

    5. Active Record关联:Rails中的ActiveRecord模型可以定义多种关联,如 belongs_to、has_many、has_one 和 has_and_belongs_to_many。在线考试系统中,试题可能 belongs_to 一个类别,考生 has_many 考试记录。 6....

    ruby多模型绑定

    在 Rails 中,有几种基本的关联类型:`belongs_to`, `has_many`, `has_one`, `has_and_belongs_to_many`等。这些关联类型可以用来表示不同模型之间的关系,例如一对多关系、多对一关系、多对多关系等。 #### 2.1 ...

    Ruby on Rails入门经典.rar

    6. **ActiveRecord associations**:定义模型之间的关系,如属主-从(has_many/belongs_to)、一对一(has_one/belongs_to)和多对多(has_and_belongs_to_many或has_many :through)。 7. **AJAX**:Rails支持无...

    ActiveRecord简单实例_activerecord.zip

    ActiveRecord还支持关联,如一对一(has_one),一对多(has_many),多对一(belongs_to)和多对多(has_and_belongs_to_many)关系。例如,如果`users`表和`posts`表有关系,可以这样定义: ```ruby class User ...

    ruby on rails 实例代码

    此外,Rails提供了强大的关联功能,如一对一(has_one)、一对多(has_many)、多对多(has_and_belongs_to_many)等,允许我们轻松处理复杂的数据关系。例如,一个用户可以有多个帖子(posts): ```ruby class ...

    has_array_of:PostgreSQL数组上的ActiveRecord关联

    该插件使用PostgreSQL数组的功能实现了在Rails中进行has_and_belongs_to_many关联的替代方法。 在很多情况下,你只需要或的功能使用许多一对多与连接表是不必要的传统方法。 我们可以只存储id的整数数组。 它是如何...

    scaffolding+will_paginate+提供will_paginate下载

    3. **模型中启用分页**:在需要分页的模型中,使用`has_many :through`或`has_and_belongs_to_many`关联时,需要在关联上添加`:finder_sql`选项,以便`will_paginate`可以正确处理分页。 4. **控制器中设置分页参数...

    Agile Web Development with Rails

    has_and_belongs_to_many :categories validates_presence_of :name, :description validates_acceptance_of :non_disclosure_agreement validates_uniqueness_of :key end ``` 这段代码展示了Rails中的一个...

    rails敏捷开发的购物车系统

    使用`has_and_belongs_to_many`或`has_many :through`关联来实现这一关系。 接下来,我们关注购物车的持久化。Rails提供了Session存储,可以用来临时存储用户的购物车信息,但这种存储方式不适用于持久保存。因此,...

    MangoDB:MongoDB KO 3.3(用于3.2、3.1和3.0检查分支)

    to,has_many和has_and_belongs_to_many 验证对象数据,包括嵌入式对象类表扩展的行为-例如Model_Ferrari扩展Model_Car扩展芒果。 其他ORM用户非常熟悉的非常简单的语法芒果数据库一个访问MongoDB的简单包装器。 将...

    amoeba:Ruby宝石,允许复制ActiveRecord对象及其关联的子对象,可通过模型上的DSL配置

    has_and_belongs_to_many 一个简单的DSL,用于配置要复制的字段。 DSL可以应用于您的Rails模型或即时使用。 支持STI(单表继承)子代继承其父变形虫设置。 多种配置样式,例如包含,排除和不

    ThinkPHP关联模型操作实例分析

    通常我们所说的关联关系包括下面三种: ◇ 一对一关联 : ONE_TO_ONE , 包括 HAS_ONE 和 BELONGS_TO ◇ 一对多关联 : ONE_TO_MANY , 包括 HAS_MANY 和 BELONGS_TO ◇ 多对多关联 : MANY_TO_MANY 关联定义 数据表...

    Ruby学习思维导图.pdf

    - **关联**:定义模型之间的关系,如 `has_many`, `belongs_to`, `has_one`, `has_and_belongs_to_many` 等。 - **验证**:确保数据的有效性。 - **回调**:在特定的时间点执行代码。 **5.4 视图和模板** - **ERB*...

Global site tag (gtag.js) - Google Analytics