Home > Software engineering >  What new method does Rails' "has_many :through" generate?
What new method does Rails' "has_many :through" generate?

Time:04-12

I am establishing an associated relationship between models for house buying and selling. User will have plural Housing agencies and plural Houses, and conversely, House will also have plural Housing agencies and plural Users. The specific models are as follows: model User

has_many :agencies
has_many :houses through: :agencies

model Agency

belongs_to :user
belongs_to :house

model House

has_many :agencies
has_many :users through: :agencies

I want to know, if I want to query the users who have joined the agency, can I do this

House.where(id: 10).agency_users

That is, will the agency_users method be generated? If yes, where is the official teaching document written? If not, how should it be fixed?

thanks

CodePudding user response:

To start with you have an error in the definition of your assocation - through is a keyword argument so you need to add a colon:

has_many :houses through: :agencies

What new method does Rails' "has_many :through" generate?

It generates a whole bunch of methods:

                                  |       |          | has_many
generated methods                 | habtm | has_many | :through
---------------------------------- ------- ---------- ----------
others                            |   X   |    X     |    X
others=(other,other,...)          |   X   |    X     |    X
other_ids                         |   X   |    X     |    X
other_ids=(id,id,...)             |   X   |    X     |    X
others<<                          |   X   |    X     |    X
# ...

I don't know where you got agency_users from but its definately not going to be generated from any of the code in your question as its based on the name argument (the first one).

You're also making a classic rookie misstake and calling an assocation method on an ActiveRecord assocation. where returns multiple records while the assocations create instance methods. Use find(10) or find_by(id: 10) (if it should be allowed to be nil).

If yes, where is the official teaching document written? If not, how should it be fixed?

I'm not sure what you're actually asking here or what "official teaching document" is supposed to mean. Rails has both API documentation and guides.

If you're talking about documenting your own code it depends on if you're using RDoc or Yard. Neither will actually automatically pick up the methods generated by the assocation macros as far as I know but you can do it manually in Yard with:

# @!method foo

I don't know if RDoc has a similiar feature so you might to just document the assocations in the overall class description.

These assocations are not what you really want

The main problem here is that you're putting house_id on the agencies table which means that it can only ever record a single value per agency! Thats not going to be good for buisness.

What you actually want is something more like

class Listing
  belongs_to :user
  belongs_to :agency
end

class Agency
  has_many :listings
  has_many :clients, 
    through: :listings,
    source: :user
end

class User
  has_many :listings
  has_many :agencies, 
    through: :listings
end

I would generally avoid the term House and go with the domain specific listing which can cover many different kinds of properties.

  • Related