How to Save Data with Has_Many: Through

Rails - has_many :through association + save data for association

I change my association type to HABTM and that's enough for my situation. So..

models:

  class Order < ActiveRecord::Base
has_and_belongs_to_many :products
before_destroy { products.clear }
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :orders
end

Order_controller:

def create
order = current_user.orders.new(date: Date.today.to_s)
@order_products = Product.where(id: order_params[:product_ids])
order.products << @order_products
if order.save
#blalblal - sucsess
else
#blabla - alert-notice
end

How to save data with has_many :through

@account = Account.new(params[:user])
@accountgame = @account.account_games.build(:game => Game.first, :score => 100)
@accountgame.save

Though I'd strongly recommend that if you start adding columns to your join-model that you call it something different eg "subscription" or "membership" or something similar. Once you add columns it stops being a join model and starts just being a regular model.

Ruby on Rails 'has_many :through', storing data

What you're doing here is working with nested form attributes. It's a bit complex, but it's also something people do often, so there are some good resources available.

I suggest you look at this post: http://www.sitepoint.com/complex-rails-forms-with-nested-attributes/

In particular, the section named 'More Complicated Relationships' specifically has an example of using nested attributes to set up a many-to-many association using has_many :through.

The key pieces (which commenters have already pointed out) are going to be accepts_nested_attributes_for :extras in your Booking model, and a f.fields_for :extras block in the view. You'll also need to modify your booking_params method to permit the nested values. There are a couple of strong parameters gotchas that you can potentially run into with that, so you may need to review the documentation.

Saving records with has_many through relationship

I am not sure why this works, but when I change

@keyword = Keyword.find_by(content: params[:content]) to

@keyword = Keyword.find_by(keyword_params), it works.

How to save data in a has_many trough relation from API?

Your submitted JSON needs to be the same as the permitted attributes in plan_params

{
"plan": {
"name": "Plan 8",
"patient_id": "3",
"description": "Plan nuevo",
"category_plans_attributes": [{
"kind": "Cena",
"portion": "12.3"
}]
}
}

Also you need to add the accepts_nested_attributes_for : category_plans to the Plan model.

Try that and check the rails app logs for errors if it does not succeed after this.

Rails has_many :through association: save instance into join table

If you use current_user.calendars.build(calendar_params), you will only get new calendar, no administration.

If you use current_user.calendars.create(calendar_params), you will get both administration and calendar saved into database.

If you want to check whether calendar is successfully saved first, I use this 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 is an error to associate calendar to user. This should be the 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

How to get data in has_many :through association?

<% @teacher.courses.each do |c| %>
<p><%= c.student.name %></p>
<p><%= c.quantity %></p>
<% end %>

How to save attributes to a has_many :through join table with no existing records to build from

I have a similar construct as yours in one of my websites, it works fine, except that I (translated to your example) have

attr_accessible :relationships_attributes
accepts_nested_attributes_for :relationships, allow_destroy: true

In my Relationship model, I then make parent_id accessible

This allows to, in the controller, to do something like

@child.relationships[0].schedule = ...

Even before saving the child

Or, alternatively, to set the schedule from your view immediately when entering the parent details

Just drop me a comment if not clear!



Related Topics



Leave a reply



Submit