weixin_42398259 2010-09-10 20:21
浏览 234
已采纳

has_and_belongs_to_many

model:

class Group < ActiveRecord::Base
has_and_belongs_to_many :users

end

class User < ActiveRecord::Base
has_and_belongs_to_many :groups
end

数据库连接表字段:
create_table "groups_users", :id => false, :force => true do |t|
t.integer "group_id"
t.integer "user_id"
end

应该是一个简单的多对多的连接表。。。

我在groups_controller里添加一个新的分组,我也知道user_id,怎么也在groups_users(没有生成模型类)里添加一条数据呢?

  • 写回答

3条回答 默认 最新

  • okela1985851010 2010-09-13 11:22
    关注

    [code="ruby"]
    class Group < ActiveRecord::Base
    has_many :groups_users
    has_many :users, :through => :groups_users

    end

    class User < ActiveRecord::Base
    has_many :groups_users
    has_many :groups , :through => :groups_users
    end

    [code="ruby"]
    group.users.delete(user)
    [/code]

    Methods Added by has_many()
    Just like belongs_to and has_one, has_many adds a number of attribute-related
    methods to its host class. Again, these methods have names that start with
    the name of the attribute. In the descriptions that follow, we’ll list the methods
    added by the declaration
    class Customer < ActiveRecord::Base
    has_many :orders
    end
    orders(force_reload=false)
    Returns an array of orders associated with this customer (which may be
    empty if there is none). The result is cached, and the database will not be
    queried again if orders had previously been fetched unless true is passed
    as a parameter.
    orders < Adds order to the list of orders associated with this customer.
    orders.push(order1, ...)
    Adds one or more order objects to the list of orders associated with this
    customer. concat is an alias for this method.
    orders.replace(order1, ...)
    Replaces the set of orders associated with this customer with the new
    set. Detects the differences between the current set of children and the
    new set, optimizing the database changes accordingly.
    orders.delete(order1, ...)
    Removes one or more order objects from the list of orders associated
    with this customer. If the association is flagged as :dependent => :destroy
    or :delete_all, each child is destroyed. Otherwise it sets their customer_id
    foreign keys to null, breaking their association.
    orders.delete_all
    Invokes the association’s delete method on all the child rows.
    orders.destroy_all
    Invokes the association’s destroy method on all the child rows.

    orders.clear
    Disassociates all orders from this customer. Like delete, this breaks the
    association but deletes the orders from the database only if they were
    marked as :dependent.
    orders.find(options...)
    Issues a regular find call, but the results are constrained to return only
    orders associated with this customer. Works with the id, the :all, and the
    :first forms.
    orders.count(options...)
    Returns the count of children. If you specified custom finder or count
    SQL, that SQL is used. Otherwise a standard Active Record count is
    used, constrained to child rows with an appropriate foreign key. Any of
    the optional arguments to count can be supplied.
    orders.size
    If you’ve already loaded the association (by accessing it), returns the size
    of that collection. Otherwise returns a count by querying the database.
    Unlike count, the size method honors any :limit option passed to has_many
    and doesn’t use finder_sql.
    orders.length
    Forces the association to be reloaded and then returns its size.
    orders.empty?
    Equivalent to orders.size.zero?.
    orders.sum(options...)
    Equivalent to calling the regular Active Record sum method (documented
    on page 315) on the rows in the association. Note that this works using
    SQL functions on rows in the database and not by iterating over the
    in-memory collection.
    orders.uniq
    Returns an array of the children with unique ids.
    orders.build(attributes={})
    Constructs a new order object, initialized using the given attributes and
    linked to the customer. It is not saved.
    orders.create(attributes={})
    Constructs and saves a new order object, initialized using the given
    attributes and linked to the customer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?