If it is, what's its defence? And if it isn't, why isn't it?
I think it is declaring new neighbor, akin to delegation. OTOH, if it were possible, it might be getting a bit bad to do multiple :through levels... (Do I remember correctly that they made this possible lately?)
Related
I am building an accommodation booking website on rails and I am confused about one aspect of the models. So normally the way I know of building it is by having a Room model that has many Reservations and the Reservation model belongs to the Room model. Problem is that this time I want the visitor to be able to book multiple rooms on one reservation. Is the Room has many Reservations association correct for that usage? Can anyone help me find a way of building that, do I have to use a single form that makes multiple records? I am sorry for my ignorance, it's the first time a concept like this fall into my hands.
Thank you all very much
You essentially need to have a many-to-many relationship defined and rails has a couple options to do so.
One option is the has_and_belongs_to_many relationship, which you can read up on here. However, my preferred option is the has_many, through: [model] approach as such:
class Room
has_many :room_reservations
has_many :reservations, through: :room_reservations
end
class Reservation
has_many :room_reservations
has_many :rooms, through: :room_reservations
end
class RoomReservation
belongs_to :room
belongs_to :reservation
end
essentially you have an intermediate table to create the many-to-many join.
I was wandering why in books like Agile Web Development with Rails there's no mention to validates_whatever_of way of validating, all validation examples are done using validates :attr, :whatever => true ? I've just started learning Rails and this made me confused!
In Rails 2.x, where you would have said something like:
validates_presence_of :user_name
in 3.x, you now do:
validates :username, :presence => true
The old way is still supported, I think, but it is deprecated.
It's really just a different way of expressing the same thing. While older books and tutorials will use the former, it should be fairly simple to translate that to 3.x style. See http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates for example.
The validates syntax is just a new shortcut for the same thing. It's especially useful when you're validating a bunch of attributes with similar limits. So this:
validates_presence_of :one
validates_presence_of :two
validates_presence_of :three
validates_presence_of :four
can be reduced to
validates :one, :two, :three, :four, :presence => true
It's also a nice, consistent interface to custom validators.
Suppose i have mapping table map_user_roles. I have defined models Role and User. Both are associated to each other by relationship has_and_belongs_to_many. Of course it does not make sense to define model for mapping table in rails.
I have defined users_controller and roles_controller for crud operations on user and role respectively.
For association of user-role, what should i do? Should i define separate controller like user_roles_controller or should i make modifications in Role and User controller(if so how to do so) ?
Please suggest, what is good practice. Examples and good links would be great help
Thanks for devoting time.
I don't see what a separate controller for the association would offer that couldn't be achieved with your existing UsersController and RolesController. Also, note that sometimes is does make sense to define a model for the mapping table, that's what the has_many :through association is for. You should use it if you need to store extra attributes against the join model.
This is more of a conceptual question...
I'm starting on an app that will have a few roles (e.g. employee, manager, store_manager). Each role will view/edit/destroy in different ways from each other. When a manager is editing an employee the view will be very different than when a store_manager is editing.
My thinking was to namespace many of the models with the role. Like:
namespace :store_manger do
resources :users
resources :widgets
end
namespace :manager do
resources :users
resources :widgets
end
This seems to be a much cleaner method than creating all the if can? :update, #article all over the controllers and views.
So, is my thinking on track?
Are there any pitfalls to using this method that I need to be aware of?
Or, is there a better way to organize this?
You're correct, grouping different roles into the same view/controllers doesn't make much sense in this case since the views are all different. Even if you need to share views later on, you can just render the same layout.
I need perform some extra logic on an object before it is saved to the database. I would assume that using a before_filter would be the correct way to accomplish this, but I'm not sure how to pass the object to be saved into my before_filter method.
This is my first ever post here so go easy on me!
Theres a before_save method, in Rails 3 at least, that can be called in the model.
in Posts model
before_save :add_url_and_ID
def add_url_and_ID
#extra logic
self[:url] = whatever.com
self[:member_id] = member.id
end
I'm probably way off but its my first go!
That all sounds like model code to me. To get the most out of Rails (or any MVC framework) follow the 'Fat Models, Skinny Controllers' rule of thumb. It can be taken too far, but I think in this case it's pretty clear. If there are errors with the helper functions you mention, shouldn't the save action fail with appropriate error messages?
There's lots of good posts on SO on this subject. Here's one
If this doesn't give you enough to work with I'd suggest posting some code.