Best practice about empty belongs_to association
I think it is absolutely normal approach.
You can just leave house_id
with null
value in database for the models which don't belong to other.
Custom searching a belongs_to assocation is empty
<%= search.input :cities, collection: City.all.order(name: :asc), as: :select, label: "City" %>
One of the reasons the above code didn't work is,
City.all.order(name: :asc)
return a ActiveRecord::Relation
object, but a collection searches for an array or range.
Collections can be arrays or ranges. from the collection documentation
Another point from that documentaion is,
when a :collection
is given the :select
input will be rendered by default, so we don't need to pass the as: :select
So, change the input to
<%= search.input :cities, collection: City.uniq.pluck(:name).sort,label: "City" %>
Association belongs_to :through
There is has_many :through
in Rails. You can check out the Rails guide to see how to use it.
Delete a belongsTo Association
why not using Foreign Keys in the database and select on DELETE CASCADE
and let the database do the work...
[Based on the comment] if you the Address is attached to other models that you dont want to delete, you can set those FK to ON DELETE RESTRICT
and the building wont be deleted.
And if you need something more complex you can add the beforeDelete() callback in your model, there's an example in the doc
Good Luck
How do I make my belongs_to field optional?
It used to be that you could just leave the value as nil
and Rails was perfectly happy. However this was changed in Rails 5.
If you want the belongs_to
to be optional, you simply have to pass optional: true
:
belongs_to :grading_rubric, optional: true
You can find more info about it here
cakephp mapping $belongsTo association to a non primary key
hmm this might not be the correct way, but i already had some similar problems and i corrected it by doing something like:
'conditions' => array(' `RentalLineitem`.`i_num` = `Inventory`.`i_num`'),
hope this helps,
Good Luck
Correct way to build multiple belongs_to association
You can accomplish what you need any number of ways, all of which being valid.
# Option 1
Content.create(data_book: DataBook.create(whatever_params), client: Client.create(whatever_params))
# Option 2
data_book = DataBook.create(whatever_params)
client = Client.create(whatever_params)
content = Content.create(data_book: data_book, client: client)
# Option 3
content = Content.new
content.data_book = DataBook.find(id)
content.client = Client.find(id)
content.save
# Options 4
content = Content.new
data_book = content.build_data_book(whatever_params)
client = content.build_client(whatever_params)
content.save
# Etc. etc. etc.
...
Related Topics
Array.Include? Multiple Values
Differencebetween a Process' Pid, Ppid, Uid, Euid, Gid and Egid
What Are the Ruby's Object#Taint and Object#Trust Methods
Difference Between .Nil, .Blank? and .Empty
Array Attribute for Ruby Model
Rails How to Switch Between Dev and Production Mode
Rails Applications' Life Cycle
Ruby: Explicit Scoping on a Class Definition
What Are the Main Differences Between Sinatra and Ramaze
Rails 3: Generate Unique Codes (Coupons)
Don't Create View Folder on Rails Generate Controller
Namespacing Thor Commands in a Standalone Ruby Executable
Confusing Behaviour of Const_Get in Ruby
Simple_Form: Remove Outer Label for an Inline Checkbox with Label