Question

I'm creating a little app to get to know rails a little better, I've used the omniauth gem to allow users to register/signin using facebook, that works great.

However, say for example I use a scaffold for the user to add an address, how can I create a relationship between the two? As I understand a user has_many :addresses and address belongs_to :user but how do I create a record when a user adds an address specifically for that user so when it comes to them logging in they only see the addresses they added?

Hope that makes sense

Thanks

KT

Was it helpful?

Solution

You'll want to scope the create using the association. This means that if User has_many :addresses, you'll want to require login in a before_filter in AddressesController. Then, inside your controller, you'll want to change lines like (from the show action):

Address.find(params[:id])

to

current_user.addresses.find(params[:id])

Similarly, you'll want to scope your create actions through the association, as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top