belongs_to association query method doesn't exist - ruby-on-rails

The "association?" query method that the Rails docs say should exist when I create a belongs_to association doesn't actually get created:
class Author < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :author
end
>> p = Post.create(:author => Author.create)
>> p.author?
NoMethodError: undefined method `author?' for #<Post:0x2555d28>
Is this a bug, are the docs wrong, or am I doing something wrong?

Take the question mark off.
p.author

It only works on boolean attributes, not belongs_to associations.

Related

undefined method build_list (Rails 5)

user.build_book does not seem to work. I get an undefined method build_book error. However,
a= user.book.build
a.save
does work. Why would that be?
rails g migration add_user_reference_to_book user:references
class Book < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_many :books
end
I would like to try explain your question,
parent.build_child(attributes) this for has_one relationship (parent has_one child)
if you using has_many then you should use parent.children.build(attributes)
if user has_one book then #user.build_book will work
if user has_many books then user #user.books.build

Rails: building object from has_many associated object

I think it might be a syntax problem.
I have a Event.rb model:
class Event < ActiveRecord::Base
belongs_to :members
has_one :brazusc
end
A Member.rb:
class Member < ActiveRecord::Base
has_many :events
end
And a Brazusc.rb model:
class Brazusc < ActiveRecord::Base
belongs_to :event
end
What I am trying to do: member.events.build_brazusc, but I get:
NoMethodError: undefined method `build_brazusc' for #<Event::ActiveRecord_Associations_CollectionProxy:0x007f99517d4a78>
How would I build an object from the has_many association?
I am also trying to retrieve the associated model from a has_many/has_one association, like this: member.events.brazusc, since brazusc has an event_id, but I get:
NoMethodError: undefined method `leads' for #<Event::ActiveRecord_Associations_CollectionProxy:0x007f99517d4a78>
How can I retrieve the object (brazusc) that is associated with event?
Any help will be much appreciated.
This would do the trick:
new_brazusc = member.events.find_by(name: "Brazusc")
new_brazusc.build_brazusc.save

Association named 'echo_producer_apis' was not found on EchoProducerApi; perhaps you misspelled it?

I think I am doing some naming error so please help me out...!!
class EchoProducerApi < ActiveRecord::Base
has_one :echo_practice_api
end
class EchoPracticeApi < ActiveRecord::Base
belongs_to :echo_producer_apis
end
#producer_data = EchoProducerApi.joins(:echo_producer_apis).select("echo_practice_apis.*,echo_producer_apis.*").paginate(:page => params[:page] , :per_page => 10)
Remember Rails relationships and associations are self-explanatory. belongs_to essentially means that an instance of your object belongs_to only one object of other class, therefore you should specify it as a singular value. Hence you should change from:
class EchoPracticeApi < ActiveRecord::Base
belongs_to :echo_producer_apis
end
To:
class EchoPracticeApi < ActiveRecord::Base
belongs_to :echo_producer_api
end
Change
belongs_to :echo_producer_apis
to
belongs_to :echo_producer_api
With belongs_to :echo_producer_apis, Rails will look for a class name called EchoPracticeApis, but you have EchoPracticeApi. That's why it is not working. Change to the association name will fix it.
Note: Generally association name for belongs_to and has_one should be a singular.

Why can't I call create on this has_one relationship?

I'm missing something basic. This is Rails 3.2.16.
In my app:
User has_one Subscription
Subscription belongs_to User
Subscription belongs_to Plan
But when I try to do this:
user.subscription.create(plan_id: plan.id)
I get the following:
NoMethodError: private method `create' called for #<Subscription:0x007f80f1f4dfc0>
Yet this works fine:
Subscription.create(user_id: user.id, plan_id: plan.id)
Perhaps you're looking for this method:
user.create_subscription(plan_id: plan.id)
From the RoR guide:
When initializing a new has_one or belongs_to association you must use the build_ prefix to build the association, rather than the association.build method that would be used for has_many or has_and_belongs_to_many associations. To create one, use the create_ prefix.
In your models you should have the associations like this:
class Subscription < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_one :subscription
end
Read here
one way to do that
user.subscription = Subscription.create(plan_id: plan.id)

rails method to get the association name of a model

Is there a way to find out what associations a model has? Take these 2 models:
class Comment < ActiveRecord::Base
belongs_to :commentable
end
class Post < ActiveRecord::Base
has_many :comments
belongs_to :user
end
I'm looking for something like:
Post.has_many #=> ['comments', ...]
Post.belongs_to # => ['user']
Comment.belongs_to # => ['commentable']
You're looking for reflect_on_all_associations.
So in short:
Post.reflect_on_all_associations(:has_many)
...will give an array (of object with attributes like name, etc) of all has_many associations.
The following will list all the associations for a particular instance of Post.
#app/models/post.rb
def list_associations
associations = []
User.reflect_on_all_associations.map(&:name).each do |assoc|
association = send assoc
associations << association if association.present?
end
associations
end

Resources