Rails has_many :through association: save instance into join table -
in our rails app, there 3 models:
class user < activerecord::base has_many :administrations, dependent: :destroy has_many :calendars, through: :administrations end class administration < activerecord::base belongs_to :user belongs_to :calendar end class calendar < activerecord::base has_many :administrations, dependent: :destroy has_many :users, through: :administrations end
and here corresponding migrations:
class createusers < activerecord::migration def change create_table :users |t| t.string :first_name t.string :last_name t.string :email t.timestamps null: false end end end class createadministrations < activerecord::migration def change create_table :administrations |t| t.references :user, index: true, foreign_key: true t.references :calendar, index: true, foreign_key: true t.string :role t.timestamps null: false end end end class createcalendars < activerecord::migration def change create_table :calendars |t| t.string :name t.timestamps null: false end end end
we have created calendar#create
action follows:
def create @calendar = current_user.calendars.build(calendar_params) if @calendar.save flash[:success] = "calendar created!" redirect_to root_url else render 'static_pages/home' end end
and corresponding _calendar_form.html.erb
partial:
<%= form_for(@calendar) |f| %> <%= render 'shared/error_messages', object: f.object %> <div class="field"> <%= f.text_field :name, placeholder: "your new calendar name" %> </div> <%= f.submit "post", class: "btn btn-primary" %> <% end %>
this "works" since, when create new calendar through form, show in rails console, when type calendar.all
.
however, seems no new @administration
being created , administration table not updated, nothing returns when type administration.all
in console.
we thought administration table, join table between user table , calendar table, , respectively contains user_id
, calendar_id
columns, updated automatically when creating new calendar.
how can achieve this? need create specific administration#create
action?
update: based on comments , answers, implemented following calendarscontroller
:
class calendarscontroller < applicationcontroller def create @calendar = current_user.calendars.build(calendar_params) if @calendar.save current_user.administrations << @calendar @calendar.administration.role = 'creator' flash[:success] = "calendar created!" redirect_to root_url else render 'static_pages/home' end end ...
however, returns following error:
activerecord::associationtypemismatch in calendarscontroller#create unless record.is_a?(reflection.klass) || record.is_a?(reflection.class_name.constantize) message = "#{reflection.class_name}(##{reflection.klass.object_id}) expected, got #{record.class}(##{record.class.object_id})" raise activerecord::associationtypemismatch, message end end app/controllers/calendars_controller.rb:6:in `create'
are missing something?
if use current_user.calendars.build(calendar_params), new calendar, no administration.
if use current_user.calendars.create(calendar_params), both administration , calendar saved database.
if want check whether calendar saved first, use approach:
def create @calendar = calendar.build(calendar_params) if @calendar.save current_user.administrations << @calendar flash[:success] = "calendar created!" redirect_to root_url else render 'static_pages/home' end end
updated:
there error associate calendar user. should correct one:
def create @calendar = current_user.calendars.build(calendar_params) if @calendar.save current_user.calendars << @calendar flash[:success] = "calendar created!" redirect_to root_url else render 'static_pages/home' end end
Comments
Post a Comment