lundi 24 octobre 2016

Retreive Values from Relationship table of has_and_belongs_to_many on Rails5

I have two tables Role and User, and I linked those two tables with has_and_belongs_to_many relationship with rails.

I'm successfully insert the data into the third table which is created by has_and_belongs_to_many relationship. Using the following code

def create
user_params[:password] = User.hash(user_params[:password])
@user = User.new(:first_name => user_params[:first_name],
                 :last_name=>user_params[:last_name],
                 :email => user_params[:email],
                 :contact_number=>user_params[:contact_number],
                 :password=>user_params[:password])

@roles = user_params[:roles];
for role in @roles
  @user.roles << Role.find(role)
end
if @user.save
  respond_to do |format|
    msg = { :status => "ok", :message => "User Creation Success!" }
    format.json  { render :json => msg }
  end
end
end

Now my problem is how do I read the valuse from the relationship table and how do I update any value to the relationship table.

Aucun commentaire:

Enregistrer un commentaire