问题描述
model:class Group < ActiveRecord::Basehas_and_belongs_to_many :usersendclass User < ActiveRecord::Basehas_and_belongs_to_many :groupsend数据库连接表字段: 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(没有生成模型类)里添加一条数据呢? 问题补充:如果是删除关系呢?group.users >> user 么??
解决方案
class Group < ActiveRecord::Base has_many :groups_usershas_many :users, :through => :groups_usersend class User < ActiveRecord::Base has_many :groups_usershas_many :groups , :through => :groups_usersend group.users.delete(user)Methods Added by has_many()Just like belongs_to and has_one, has_many adds a number of attribute-relatedmethods to its host class. Again, these methods have names that start withthe name of the attribute. In the descriptions that follow, we’ll list the methodsadded by the declarationclass Customer < ActiveRecord::Basehas_manyrdersendorders(force_reload=false)Returns an array of orders associated with this customer (which may beempty if there is none). The result is cached, and the database will not bequeried again if orders had previously been fetched unless true is passedas a parameter.orders <<orderAdds 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 thiscustomer. concat is an alias for this method.orders.replace(order1, ...)Replaces the set of orders associated with this customer with the newset. Detects the differences between the current set of children and thenew set, optimizing the database changes accordingly.orders.delete(order1, ...)Removes one or more order objects from the list of orders associatedwith this customer. If the association is flagged as :dependent => :destroyor :delete_all, each child is destroyed. Otherwise it sets their customer_idforeign keys to null, breaking their association.orders.delete_allInvokes the association’s delete method on all the child rows.orders.destroy_allInvokes the association’s destroy method on all the child rows.orders.clearDisassociates all orders from this customer. Like delete, this breaks theassociation but deletes the orders from the database only if they weremarked as :dependent.orders.find(options...)Issues a regular find call, but the results are constrained to return onlyorders 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 countSQL, that SQL is used. Otherwise a standard Active Record count isused, constrained to child rows with an appropriate foreign key. Any ofthe optional arguments to count can be supplied.orders.sizeIf you’ve already loaded the association (by accessing it), returns the sizeof that collection. Otherwise returns a count by querying the database.Unlike count, the size method honors any :limit option passed to has_manyand doesn’t use finder_sql.orders.lengthForces 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 (documentedon page 315) on the rows in the association. Note that this works usingSQL functions on rows in the database and not by iterating over thein-memory collection.orders.uniqReturns an array of the children with unique ids.orders.build(attributes={})Constructs a new order object, initialized using the given attributes andlinked to the customer. It is not saved.orders.create(attributes={})Constructs and saves a new order object, initialized using the givenattributes and linked to the customer.
解决方案二:
group=Group.create(..)user=User.new(..)group.users << user;
解决方案三:
试试group.users << user