Friday 15 February 2013

ruby on rails - HABTM filter_or_create usage -


This question is asking a similar thing: But I'm new to rail and let me apply it to my problem.

I can easily create a new collaboration between a new location and user and location:

  @location = location.new ( Params [: location] .merge (: user_ids => current_user.id))   

How can I change it:

  • New Location and association is created if location is not present
  • location already exists, then a new union has been created.

    ?

    My models are like this:

    location :

      class space < ActiveRecord :: Base attr_accessible: name ,: user_ids has_and_belongs_to_many: user expired   

    user :

      class user & lt; ActiveRecord :: Base attr_accessible: Email, Password: password_confirmation: remember_me has_and_belongs_to_many: end of locations   

    So I want to know how to create a union and how to check what I want to do Should Just An Association

    There is only location_id and user_id fields in my association table.

    Many thanks.

    You want to take a look at: AR :: B # first_or_sartay / AR :: B # First_or_initialize

    No comments:

    Post a Comment